如何自动弹出键盘?

3

我有一个编辑文本框,需要输入密码,但是我必须点击这个字段。如何在不触摸编辑文本的情况下自动弹出键盘?

有一个编辑文本的XML字段:

<EditText
android:id="@+id/editPasswd"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:password="true"/>
3个回答

8

在您想要显示键盘的位置(可能在oCreate中)使用此代码

    EditText myEditText = (EditText) findViewById(R.id.editPasswd);

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
        .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);

3
这对我有用: <activity android:name=".first" android:windowSoftInputMode="stateAlwaysVisible"> 意思是:这个代码对我有用: 在名为“first”的活动中,键盘始终可见。 - Juronis

5
我假设您想在活动开始时自动弹出它。如果是这样,请将以下内容添加到清单文件中的 activity-标签中,这对我有用(不像Erfan的答案):
android:windowSoftInputMode="stateVisible" 

1

Java类中的一行:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接