弹出AlertDialog时显示键盘

时间:2013-03-27 00:36:37

标签: java android android-softkeyboard

我在SO上研究了很多答案,并在我的Dialog弹出时提出以下代码来显示键盘:

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Title");
    final EditText input = new EditText(this);
    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean focused) {
            alertDialog
                    .getWindow()
                    .setSoftInputMode(
                            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        }
    });
    input.setFocusable(true);
    input.requestFocus();
    alertDialog.setView(input);

            alertDialog.show();

对话框显示,但键盘没有弹出。如果这会产生影响,这都在onTouch(...)方法中。

我的应用仅限横向模式。我发现在纵向模式下,它正在显示。这是为什么?

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

看起来像横向模式让我失望了。以下代码立即解决了问题:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);
                imm.showSoftInput(input, InputMethodManager.SHOW_FORCED);
                alertDialog
                        .getWindow()
                        .setSoftInputMode(
                                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);