我正在使用AlertDialog显示一个输入框。当我调用AlertDialog.show()时,对话框内部的EditText会自动聚焦,但软键盘不会自动显示。

我如何使软键盘自动显示时,对话框显示?(并且没有物理/硬件键盘)。类似于当我按下Search按钮调用全局搜索时,会自动显示软键盘。


当前回答

尝试了很多,但这对我来说是有效的(kotlin):

        val dialog = builder.create()
        dialog.setOnShowListener {
            nameEditText.requestFocus()
            val s = ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
            s?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
        }

        dialog.setOnDismissListener {
            val s = ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
            s?.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
        }

        dialog.show()

其他回答

试着使用:

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

在AlertDialog中从EditText显示软键盘的问题可能是在AlertDialog.show()中,因为在显示AlertDialog之后应用了EditText。我不确定所有版本的API都是这样,但我认为解决方案来自API级别21,因为它来自AlertDialog.create(),它应该在AlertDialog.show()之前调用。

这是我对这种情况的最佳解决方案。首先,在某个地方创建:

    private int showKeyboard(View view) {
        final InputMethodManager inputManager = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            boolean isShown = inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); // flag=InputMethodManager.SHOW_IMPLICIT ili =
            return (isShown) ? 1: -1;
        }
        return 0;
    }

然后,在AlertDialog之后。生成器生成器= new AlertDialog.Builder(requireContext());继续:

    EditText editText = new EditText(requireContext());
    builder.setView(editText);
    // ... put positive-negative buttons, and etc ...
    AlertDialog dialog = builder.create(); // Create dialog from builder
    dialog.setCancelable(false); // If you need
    Handler handler = new Handler();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) { // 1. When a dialog is displayed
            editText.requestFocus();
            Runnable runnable = new Runnable() { // create one runnable
                int counter = 0;
                public void run() {
                    int status = showKeyboard(editText); // 2. Call func.above for keyboard, but...
                    if(status == -1 && counter < 10){ handler.postDelayed(this, 100); counter ++; } // ...if it inst shown call again after 100ms
                }
            };
            runnable.run(); // Execute runnable first time here
        }
    });
    dialog.show();

不要忘记导入android.os.Handler;等等;-)

感谢大家的投票。

是的,你可以用setOnFocusChangeListener来做,它会帮助你。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

你可以在EditText上附加onFocusListener或在AlertDialog上附加onShowListener,并添加以下内容:

val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)

这对我很有用。我为对话框设置了一个OnShowListener,在那个监听器中,我为文本字段设置了一个onFocusChangeListener,在那个FocusChangeListener中,我用100ms的延迟调用showSoftInput,然后删除FocusChangeListener,所以它只处理第一个焦点变化。

当调用dialog.show()时,OnShowListener被调用,它请求文本字段的焦点。当文本字段获得焦点时,将调用FocusChangeListener并最终显示键盘。

    val views = DialogBinding.inflate(layoutInflater) // layout with 'textfield'
    val dialog = MaterialAlertDialogBuilder(requireContext())
        .setTitle("Exercise")
        .setView(views.root)
        ...
        .create()
    dialog.setOnShowListener {
        views.textfield.onFocusChangeListener = View.OnFocusChangeListener { view, _ ->
            view.postDelayed({
                (view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(view, 0)
            }, 100)                
            views.textfield.onFocusChangeListener = null
        }
        views.repcount.requestFocus()
    }
    dialog.show()