如何始终显示软键盘并且不要让它关闭?

时间:2017-12-15 05:51:52

标签: android android-edittext keyboard android-keypad window-soft-input-mode

我想知道两件事

  1. 如何始终显示软键盘并且不要让它关闭(即使按下后退或OK按钮)?

  2. 如何从中获取输入?

  3. 我已经尝试过这段代码:

        EditText yourEditText= (EditText) findViewById(R.id.ed);
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    

    有这些变种:

    1. imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
    2. imm.toggleSoftInputFromWindow( yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
    3. imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

1 个答案:

答案 0 :(得分:2)

android:windowSoftInputMode="stateAlwaysVisible"用于AndroidManifest.xml文件

喜欢这样:

<activity android:name=".YourActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />  // OR stateVisible
  

如果该活动具有EditText,那么何时活动将开始   你的键盘自动打开

如果您想在完成任何操作后仍然打开Keyboad,请通过 programetically

执行此操作
InputMethodManager imm =
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(
    linearLayout.getApplicationWindowToken(),
    InputMethodManager.SHOW_FORCED, 0);

显示软键盘

InputMethodManager imm = (InputMethodManager)
     getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,
     InputMethodManager.SHOW_IMPLICIT);

EDITABLE_VIEW可以是任何关注屏幕的视图,例如

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

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(editText.getWindowToken(), 0);

Documentation