AlertDialog与EditText,自动打开软键盘并将焦点放在EditText上无法正常工作。

40

我试图让一个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();

我尝试了许多方法,但都没成功。希望你们能在这里帮助我。提前感谢!


请查看此链接:https://stackoverflow.com/a/76074557/12272687 - Mori
6个回答

73

好的,我设法让它工作了:

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()来显示对话框,由Sabre提供的代码打开软键盘。每个按钮中的另一个代码片段会自动关闭软键盘。


太棒了!一直在尝试让我的警告对话框中的EditText显示键盘,但是一直无法实现。非常感谢! - gbotha
谢谢。这对我很有帮助。 - S.J. Lim
这个方法可以用,但请记住必须使用builder.setView(),因为如果你使用dialog.setContentView(),键盘会出现在对话框下面,无法捕捉输入。 - GuilhE
1
它可以工作,但是如果用户没有按下肯定或否定按钮,而是点击了白色空间,那么键盘会在对话框关闭后保持打开状态。 - smj

16
你可以使用以下代码替代原来的代码: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(); 之后调用此函数。


1
我的回答对您有帮助吗? - Sabre
这个可以工作,但我无法在textView上显示光标。有什么想法吗? - adev
实际上,它运行得很好,但是在调用dialog.show()之前调用了dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)。 - dorsz
我的AlertDialog在一个独立的DialogFragment类中。使用InputMethodManager方法在许多设备上都有效,但不是所有设备都有效。对我来说,使用alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);使其在所有设备上都有效。 - Eric B.
这解决了我的问题,我只需要第一行代码。 - 476rick

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 中打开键盘:

    @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);
    }

0

尝试延迟一秒后再显示它 -

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

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

    dialog.show();  
}, 1000)

0

在尝试了不同的建议后,最终清除标志并在窗口上设置软输入模式是有效的。确保在dialog.show()之后调用此方法:

dialog.window?.apply {
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

0

这是对我起作用的。请求焦点于编辑文本,然后在其上发布延迟任务以显示键盘。

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

使用SHOW_FORCED会保持键盘一直显示,直到用户手动关闭它,即使他们已经关闭了对话框,因此请使用SHOW_IMPLICIT


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接