繁体   English   中英

AlertDialog弹出时显示键盘

[英]Show Keyboard when AlertDialog pops up

我研究了许多关于SO的答案,并提出了以下代码来在弹出对话框时显示键盘:

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(...)方法内。

我的应用程序仅是横向模式。 我发现它以纵向模式显示。 为什么是这样?

任何帮助表示赞赏。

好像是风景模式让我失望了。 以下代码立即解决了该问题:

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

暂无
暂无

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

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