【问题标题】:android edittext keyboard not showing after dialog dismissalandroid edittext键盘在对话框关闭后不显示
【发布时间】:2019-11-16 12:55:06
【问题描述】:

我有一个工作正常的编辑文本,但在关闭对话框后,尽管编辑文本已聚焦并显示光标,但键盘未显示

我搜索了很多,并尝试了许多没有解决我的问题的解决方案

我尝试了这些方法,但它们不起作用

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);
}

并尝试将它们添加到可运行文件中

【问题讨论】:

  • 您能否发布您正在使用的与关闭对话框和使用 EditText 相关的代码?
  • 请在警报对话框中显示您的代码。
  • @cpgreen2 和@Beverly Castillo 对话框是自定义的,并在其构造函数中扩展了 AppCompatDialog ``` if (this.getWindow() != null) { this.getWindow().setBackgroundDrawable(new ColorDrawable(颜色.透明)); this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); this.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); } ``` 它包含一个取消按钮来关闭 ``` cancelButton.setOnClickListener(view -> dismiss()); ```

标签: android android-edittext keyboard focus


【解决方案1】:

这是我用来显示编辑文本键盘的方法。

kotlin 代码:

下面是一个kotlin的扩展函数,只需要调用edittext.showKeyboard()

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

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);
      }
    });
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2013-08-23
    • 2021-01-27
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多