带有EditText的AlertDialog,自动打开软键盘并专注于EditText不起作用

时间:2012-10-21 11:11:32

标签: android focus android-edittext android-softkeyboard android-alertdialog

我正在尝试获得一段代码工作,一旦它显示然后自动打开软键盘,它应该将EditText集中在AlertDialog中。相反,它只会使屏幕变暗。

Builder builder = new Builder(this);
final EditText input = new EditText(this);
AlertDialog dialog = builder.create();
builder
    .setTitle(R.string.dialog_title_addsubject)
    .setMessage(R.string.dialog_addsubject)
    .setView(input)
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String value = input.getText().toString();
            if (input.getText().toString().trim().length() == 0) {
                Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
            } else {
                db.insertSubject(value);
                getData();
            }
         }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    input.requestFocus();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();

我尝试了很多方法但没有办法。我希望你们能在这里帮助我。提前谢谢!

5 个答案:

答案 0 :(得分:67)

好的,我设法让它运转起来:

Builder builder = new Builder(this);
            final EditText input = new EditText(this);
            builder
                .setTitle(R.string.dialog_title_addsubject)
                .setMessage(R.string.dialog_addsubject)
                .setView(input)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        String value = input.getText().toString();
                        if (input.getText().toString().trim().length() == 0) {
                            Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
                        } else {
                            db.insertSubject(value);
                            getData();
                        }
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }

                });

                builder.show();
                input.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

此方法不需要对话框,因此我可以使用builder.show()来显示对话框,Saber提供的代码打开软键盘。每个按钮中的另一个代码片段会自动关闭软键盘。

答案 1 :(得分:12)

您可以使用此代替 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

dialog.show();

之后调用此方法

答案 2 :(得分:2)

   public void selectContact(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.icon);
        builder.setTitle(R.string.title);
        builder.setPositiveButton(android.R.string.ok, context);
        builder.setNegativeButton(android.R.string.cancel,context);
        builder.setView(View.inflate(context,
                R.layout.dialog, null));
        AlertDialog alertDialog = builder.create();

        alertDialog.setOnShowListener(this); //Add listener
        alertDialog.show();
    }

在onShow中打开keyborad: -

    @Override
    public void onShow(DialogInterface dialog) {
        EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.number);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }

答案 3 :(得分:0)

尝试在一秒钟后显示 -

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    input.requestFocus();

    dialog.getWindow().
  setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    dialog.show();  
}, 1000)

答案 4 :(得分:0)

这对我有用。请求将重点放在编辑文本上,然后在其上发布延迟的任务以显示键盘。

editTextView.requestFocus()
editTextView.postDelayed(200) {
    ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
        ?.showSoftInput(
            editTextView,
            InputMethodManager.SHOW_IMPLICIT,
        )
}

使用SHOW_FORCED可以保留键盘,直到用户手动关闭它为止,即使他们已经关闭了该对话框,也请使用SHOW_IMPLICIT