片段启动时显示edittext的键盘

hee*_*ero 51 android android-softkeyboard android-edittext

当我的片段开始时,我希望我的edittext成为焦点/让用户开始输入它.我能够通过requestFocus()获得焦点,但我无法让键盘显示出来.

我试过这两个:

edit = (EditText) view.findViewById(R.id.search);
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
Run Code Online (Sandbox Code Playgroud)

edit = (EditText) view.findViewById(R.id.search);
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
edit.requestFocus();
Run Code Online (Sandbox Code Playgroud)

如何让键盘显示EditText?

Sam*_*Sam 91

这有用吗?

imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)

  • 这感觉就像一个解决方法.正确的程序应该是OP建议的,或者调用`imgr.showSoftInput(getView(),InputMethodManager.SHOW_IMPLICIT)`.切换实际上会关闭键盘*如果它已经打开了.所描述的解决方案是我唯一可以使用的解决方案,所以这是框架中的错误吗?更好的解决方案或解释将不胜感激. (3认同)

小智 11

你可以试试这个

@Override
public void onResume() {
    super.onResume();
    edit.post(new Runnable() {
        @Override
        public void run() {
            edit.requestFocus();
            InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imgr.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)


ces*_*rds 8

由于使用showSoftInput不适用于所有情况,并尝试了这里提到的一些解决方案,如:

if (binding.account.requestFocus()) {
  getActivity().getWindow()
      .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

我终于使用了:

if (binding.account.requestFocus()) {
  ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
      InputMethodManager.SHOW_FORCED,
      InputMethodManager.HIDE_IMPLICIT_ONLY
  );
}
Run Code Online (Sandbox Code Playgroud)

以来:

 binding.account.requestFocus()
Run Code Online (Sandbox Code Playgroud)

只请求焦点EditText(它不打开键盘)

((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
      InputMethodManager.SHOW_FORCED,
      InputMethodManager.HIDE_IMPLICIT_ONLY
  );
Run Code Online (Sandbox Code Playgroud)

是唯一能够正常显示键盘的解决方案(也是投票最多的键盘)

祝好运!:-)


Raf*_*ols 6

我对此有帮助的扩展:

fun EditText.showKeyboard() {
    if (requestFocus()) {
        (getActivity()?.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
            .showSoftInput(this, SHOW_IMPLICIT)
        setSelection(text.length)
    }
}
Run Code Online (Sandbox Code Playgroud)

您还将需要以下一个:

fun View.getActivity(): AppCompatActivity?{
    var context = this.context
    while (context is ContextWrapper) {
        if (context is AppCompatActivity) {
            return context
        }
        context = context.baseContext
    }
    return null
}
Run Code Online (Sandbox Code Playgroud)


Ami*_*ian 5

在尝试了此处的所有解决方案以及其他相关问题后,这是适合我的方法:

editText.postDelayed(Runnable { showKeyboard(activity, editText)} , 50)


fun showKeyboard(activity: Activity, editText: EditText) {
    val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    editText.requestFocus()
    inputMethodManager.showSoftInput(this, 0)
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,当你在没有 postDelayed 的情况下调用它时,它不会工作,即使你只是延迟 1 毫秒,它仍然不会工作:D

您还可以将其用作扩展,如下所示:

fun EditText.showKeyboard(activity: Activity) {
    val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    requestFocus()
    inputMethodManager.showSoftInput(this, 0)
}
Run Code Online (Sandbox Code Playgroud)

如果您不喜欢将活动作为参数传递,则可以按照@Rafols的建议使用此扩展函数:

fun View.getActivity(): AppCompatActivity? {
    var context = this.context
    while (context is ContextWrapper) {
        if (context is AppCompatActivity) {
            return context
        }
        context = context.baseContext
    }
    return null
}
Run Code Online (Sandbox Code Playgroud)

那么你的方法将如下所示:

fun EditText.showKeyboard() {
    val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    requestFocus()
    inputMethodManager.showSoftInput(this, 0)
}
Run Code Online (Sandbox Code Playgroud)

另外,如果您的 EditText 中已有文本,最好在现有文本的末尾设置选择:

fun EditText.showKeyboard() {
    val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    requestFocus()
    inputMethodManager.showSoftInput(this, 0)
    setSelection(length())
}
Run Code Online (Sandbox Code Playgroud)

如果你想在片段启动时启动它:

override fun onResume() {
    super.onResume()
    editText.postDelayed(Runnable { editText.showKeyboard()} , 50)
}
Run Code Online (Sandbox Code Playgroud)