【问题标题】:android:windowSoftInputMode="stateVisible" doesn't workandroid:windowSoftInputMode="stateVisible" 不起作用
【发布时间】:2012-03-18 16:34:39
【问题描述】:

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

android:windowSoftInputMode="stateVisible" 

没用。

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

  1. 将 windowSoftInputMode 添加到清单中。
  2. 在那之后不起作用,我在布局中添加了一个 EditView 字段
  3. 后来不行,我加了

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) 到 onCreate 过程。

我用 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' 元素/标签属性。
  • 你是对的 - 我改变了它。还是没有键盘。
  • 你做了什么?终于解决了吗?
  • 差不多 8 年前,但我看到这检查了 Muky 的答案。这可能是我使用的特定键盘的错误

标签: android android-layout android-emulator


【解决方案1】:

你使用默认的安卓键盘吗?如果你这样做,请在其他设备上尝试,我知道它有一些问题

【讨论】:

  • 你是对的。在我的设备上安装了外部键盘后,我的默认 KB 不知何故从设备设置中消失了。显然这是一个已知问题,它也影响了这一点。我使用了不同的设备及其工作。我仍然不知道为什么模拟器不显示 KB。
【解决方案2】:

这很简单。 我已经完成了,它可以满足您的要求。

  1. 不要对清单做任何事情,在创建新项目时保持原样。

  2. 现在定义输入管理器。

    public static InputMethodManager imm;
    imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    
  3. 现在,工资 EditText 是我的 EditText,我在该活动开始时显示键盘。

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

这将帮助您在活动开始时显示键盘。

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

在您完成活动时将此代码涂白。

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

希望它能解决您的问题。如果没有,请告诉我。

享受。 :)

【讨论】:

  • @kenji 如果此答案不适合您,我建议请遵循已接受的答案。谢谢。
【解决方案3】:

您可以通过运行强制首先关注EditText 视图:

final EditText edit = (EditText) findViewById(R.id.editText1);
edit.post(new Runnable() {
    @Override
    public void run() {
        edit.requestFocus();
    }
});

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

【讨论】:

  • 不需要创建不必要的线程。只需查看我的解决方案即可。
  • post 不会创建新线程,它会在视图的队列中添加一个 runnable,它非常高效。我仍然会使用我的解决方案,对我来说似乎更优雅。
【解决方案4】:

我发现here,您可以通过执行以下操作在活动开始时显示键盘:

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

我将他们的示例代码更改为具有您的 EditText 的 ID,这样应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    相关资源
    最近更新 更多