我创建了一个简单的应用程序来测试以下功能。当我的活动启动时,需要在软键盘打开的情况下启动。Android showSoftInput软键盘无法使用?
我的代码不起作用?!
我已经尝试清单中的各种“状态”设置和InputMethodManager(imm)代码中的不同标志。
我已将该设置包含在AndroidManifest.xml中,并在唯一活动的onCreate中显式调用。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.android.studyIme"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StudyImeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
...主要布局(main.xml中)...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/edit_sample_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hello"
android:inputType="textShortMessage"
/>
</LinearLayout>
...和...代码
public class StudyImeActivity extends Activity {
private EditText mEditTextStudy;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditTextStudy = (EditText) findViewById(R.id.edit_study);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditTextStudy, InputMethodManager.SHOW_FORCED);
}
}
嗯...我刚刚在我的Sprint LG Optimus手机上试过这个,只有默认的“HelloWorld”活动(即不包括SHOW_FORCED代码),并且按预期工作。该功能是否可能与设备(操作系统安装)相关?当我回家时,我会再次在我的其他设备(HTC,G2和MyTouch)上进行测试。 – mobibob 2011-04-02 21:10:30
请在这里看到我的回复,我已经尝试了所有下面提到的技术,但这工作:http://stackoverflow.com/a/37529370/3900270 – strangetimes 2016-05-30 15:39:38