【发布时间】:2011-07-28 01:34:01
【问题描述】:
我创建了一个简单的应用程序来测试以下功能。当我的活动启动时,它需要在软键盘打开的情况下启动。
我的代码不起作用?!
我尝试了清单中的各种“状态”设置以及 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)上再次测试。
-
请在此处查看我的回复,我已经尝试了下面提到的所有技术,但都奏效了:stackoverflow.com/a/37529370/3900270
标签: android show android-softkeyboard android-input-method