安卓:android:windowSoftInputMode="stateVisible"无效

10

在Activity启动时我想打开软键盘,我发现:

android:windowSoftInputMode="stateVisible" 

无法工作。

为了确认,我创建了一个新项目(默认的“Hello world”),并进行了以下操作:

  1. 向清单文件中添加了windowSoftInputMode。
  2. 在那之后仍然无效,我向布局中添加了一个EditView字段。
  3. 在那之后仍然无效,我向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);

    }
}

我认为'android:windowSoftInputMode'是'activity'元素/标签属性。 - Deucalion
你说得对 - 我改了。但还是没有键盘。 - Asaf Pinhassi
你做了什么?问题最终解决了吗? - Mahdi
大约8年前,我看到了Muky的答案。这可能是我使用的特定键盘中的一个错误。 - Asaf Pinhassi
4个回答

3

您是否使用默认的安卓键盘?如果是,尝试在不同的设备上使用它,我知道它有一些问题。


你说得对。在我给我的设备安装了一个外部键盘之后,一些默认的键盘设置从设备中消失了。显然这是一个已知的问题,它也影响了这个问题。我换了一台不同的设备,它能工作。我仍然不知道为什么模拟器不显示键盘。 - Asaf Pinhassi

3

这是一件简单的事情。 我已经完成了它,并且按照您的要求工作。

  1. Don't do anything with the manifest, leave it as it is while you creating the new new project.

  2. Now define inputmanager.

    public static InputMethodManager imm;
    imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    
  3. Now, here salary EditText is my EditText and i am showing the keyboard on the start of that activity.

    salaryEditText.setHint("select Salary/Wage");
    
    salaryEditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2)}); // Ignore this line
    
    if(!(imm==null))
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,1);
    

这将帮助您在activity启动时显示键盘。

要在activity结束时关闭键盘,请参见以下代码:

在完成activity时使用以下代码。

imm.hideSoftInputFromWindow(salaryEditText.getWindowToken(), 0);

希望这能解决你的问题。如果不能,请告诉我。
祝愉快。 :)

@kenji 我建议你如果这个答案不适用于你,请遵循被接受的答案。谢谢。 - Shreyash Mahajan

2
您可以通过运行以下命令来强制将焦点放在EditText视图上:EditText.requestFocus()
final EditText edit = (EditText) findViewById(R.id.editText1);
edit.post(new Runnable() {
    @Override
    public void run() {
        edit.requestFocus();
    }
});

这应该在活动开始时打开键盘。


不需要创建不必要的线程。只需查看我的答案以获取解决方案。 - Shreyash Mahajan
发布不会创建新的线程,它只是将可运行对象添加到视图队列中,非常高效。 我仍然会选择我的解决方案,我认为更加优雅。 - marmor

1

正如我在这里发现的那样,您可以通过以下方式在活动启动时显示键盘:

    EditText editText = (EditText) findViewById(R.id.editText1);
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

我已经修改了他们的示例代码,将您的EditText ID添加进去,这样应该可以正常工作。


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