在Activity启动时我想打开软键盘,我发现:
android:windowSoftInputMode="stateVisible"
无法工作。
为了确认,我创建了一个新项目(默认的“Hello world”),并进行了以下操作:
- 向清单文件中添加了windowSoftInputMode。
- 在那之后仍然无效,我向布局中添加了一个EditView字段。
- 在那之后仍然无效,我向onCreate过程中添加了
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
我使用Android2.3.3编译它,并尝试在我的Galaxy S2设备和Android4模拟器上运行它,但仍然没有键盘。
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".HelloworldActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
我的代码:public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}