首页 > 解决方案 > 在 AlertDialog 上自动打开软键盘

问题描述

我正在创建一个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();
}

我尝试使用从另一个帖子链接中获得的建议来解决:

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

但我仍然必须点击EditText打开键盘。任何想法?

标签: androidandroid-alertdialogandroid-softkeyboard

解决方案


推荐阅读