使用带有EditText的AlertDialog.Builder时,软键盘不会弹出

nir*_*ros 115 android android-softkeyboard android-input-method

我使用AlertDialog.Builder来创建一个输入框,使用EditText作为输入方法.

遗憾的是,虽然EditText处于焦点状态,但软键盘不会弹出,除非您再次明确触摸它.

有没有办法强迫它弹出?

在(AlertDialog.Builder).show()之后尝试了以下内容.但无济于事.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

谢谢!!

gri*_*4ka 221

我做了这样的事

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢.我已经搜索了一段时间,这就是你想要的方式.所有的'OnFocusChangeListener`方法对我来说似乎都很重要并且会造成麻烦.你必须从`AlertDialog.Builder`创建`AlertDialog`! (3认同)
  • 如果它不起作用,请添加行“edittext.requestFocus()”,它对我有用 (2认同)

Ale*_*sis 27

我设法像这样解决它:

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)


Phu*_*eat 23

我发现相同的代码在平板电脑上正常工作,键盘确实弹出,但在手机上没有,所以进一步研究似乎指向"调整"选项.

我正在使用它,感觉更干净.

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();
Run Code Online (Sandbox Code Playgroud)


vov*_*ost 10

在我的情况下,在显示对话框时我能够显示键盘的唯一方法是添加到我的DialogFragment:

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}
Run Code Online (Sandbox Code Playgroud)

请注意SOFT_INPUT_STATE_ALWAYS_VISIBLE而不是SOFT_INPUT_STATE_VISIBLE.

来自文档:

int SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode: please always make the soft input area visible when this window receives input focus.
Run Code Online (Sandbox Code Playgroud)


小智 6

当您调用showDialog以显示使用onCreateDialog中的AlertDialog创建的对话时

你应该把代码放在这里

    @Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });

}
Run Code Online (Sandbox Code Playgroud)


Aur*_*hie 6

就我而言,当我在显示对话框之前(创建对话框之后)设置 SoftInputMode 时,它​​没有显示。下面的代码对我有用,我在显示对话框后设置了 SoftInputMode。

科特林:

val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                .create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}
Run Code Online (Sandbox Code Playgroud)

爪哇:

AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                .create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助任何与我遇到同样问题的人。


sul*_*lai 5

这里给出一个更好的解决方案.

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Run Code Online (Sandbox Code Playgroud)

没有解决方法.EditText表现如预期.


dha*_*g23 1

这已经在这里得到了回答。使用 OnFocusChangeListener 对我有用。