在Dialog中显示键盘

sve*_*ija 4 android android-edittext

我有这段代码(RelativeLayout只是我主要布局中的一行,并不重要).

RelativeLayout cellphoneNumberLayout = (RelativeLayout) findViewById(R.id.cellphone_number);
        cellphoneNumberLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingsDialog myDialog = new SettingsDialog(Main.this);
                myDialog.show();
            }
        });
Run Code Online (Sandbox Code Playgroud)

在我的自定义对话框(SettingsDialog)中,我有EditText和一个Button.当显示对话框时,如何强制键盘立即打开并关注我的(单个)EditText字段?

我尝试使用经典的"强制",我在这里找到了SO,但这不是活动,而是一个对话框.

编辑:我试过这个,但它不起作用.将myDialog声明为类变量并添加到myDialog.show()下面;

myDialog.myEditTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                            myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        }
                    }
                });
Run Code Online (Sandbox Code Playgroud)

什么都没发生.

A. *_*iri 6

以下将在editText聚焦时显示editText的键盘:

EditText editText;
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused)
    {
        if (focused)
        {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然后将editText设置为focus:

editText.setFocusable(true);
editText.requestFocus();
Run Code Online (Sandbox Code Playgroud)