简体   繁体   English

AlertDialog弹出时显示键盘

[英]Show Keyboard when AlertDialog pops up

I've researched many answers on SO and come up with the following code to show the keyboard when my Dialog 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();

The dialog Shows, but the keyboard doesn't pop up. 显示对话框,但键盘没有弹出。 This is all within an onTouch(...) method if that makes a difference. 如果这有所不同,则全部在onTouch(...)方法内。

My app is landscape mode only. 我的应用程序仅是横向模式。 I find that in portrait mode, it is showing. 我发现它以纵向模式显示。 Why is this? 为什么是这样?

Any help is appreciated. 任何帮助表示赞赏。

It looks like it was landscape mode that was throwing me off. 好像是风景模式让我失望了。 The following piece of code solved the problem immediately: 以下代码立即解决了该问题:

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