简体   繁体   English

对话框解雇后未显示android edittext键盘

[英]android edittext keyboard not showing after dialog dismissal

I have an edit text that is working fine but after dismissing a dialog the keyboard is not showing despite the edit text is focused and the cursor is shown 我有一个可以正常工作的编辑文本,但是在关闭对话框后,尽管已将编辑文本聚焦并显示了光标,但键盘仍未显示

I searched a lot and tried many solutions that didn't fix my issue 我进行了很多搜索并尝试了许多无法解决问题的解决方案

i tried these methods but they didn't work 我尝试了这些方法,但是没有用

public void showKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

public void closeKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

and also tried adding them inside a runnable 并尝试将它们添加到可运行对象中

here is the method I use to show keyboard for an edittext. 这是我用来显示键盘的文本的方法。

kotlin code: Kotlin代码:

below is an extension function for kotlin just need to call edittext.showKeyboard() 以下是Kotlin的扩展功能,只需要调用edittext.showKeyboard()

fun EditText.showKeyboard() {
  post {
    requestFocus()
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
  }
}

the java code: Java代码:

public static void showKeyboard(EditText editText) {
    editText.post(new Runnable() {
      @Override
      public void run() {
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) editText.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
      }
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM