在AlertDialog上自动打开软键盘

时间:2019-06-14 14:34:08

标签: android android-alertdialog android-softkeyboard

我正在创建一个AlertDialog.Builder,在其中分配一个EditText以获取输入。 当它打开时,键盘不会自动显示。 这是我的代码:

private void AskForInput(int inputType, String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title);

    final EditText input = new EditText(this);
    input.setInputType(inputType);
    builder.setView(input);

    builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            keyNumberValue = input.getText().toString() + "#";
            //TODO: do things...
        }
    });
    builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.show();
}

我尝试使用从另一篇link中获得的建议来解决:

AlertDialog alertToShow = builder.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();

但是我仍然必须点击EditText来打开键盘。 有想法吗?

0 个答案:

没有答案