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

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


当前回答

这对我很有用。我为对话框设置了一个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()

其他回答

我的方法使用了Android 11+的新方法,也支持旧版本:

fun Fragment.showKeyboard() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        ViewCompat.getWindowInsetsController(requireView())?.show(WindowInsetsCompat.Type.ime())
    } else {
        val focusedView = view?.findFocus() ?: view?.apply { requestFocus() }
        val imm = (context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
        val isShowSucceeded = imm.showSoftInput(focusedView, InputMethodManager.SHOW_IMPLICIT)
        if(!isShowSucceeded) {
            imm.toggleSoftInputFromWindow(
                view?.windowToken, 0, InputMethodManager.HIDE_IMPLICIT_ONLY)
    }
}

}

edittext.setShowSoftInputOnFocus(真正的);是关于这个问题的最流畅的解决方案。

AlertDialog.Builder alertSearch = new AlertDialog.Builder(activity);
alertSearch.setIcon(R.drawable.ic_launcher);
alertSearch.setTitle("Search");
final EditText edittext = new EditText(activity);
edittext.setSingleLine();
edittext.setHint("...");
edittext.setText(SearchString);

在聚焦时设置键盘

edittext.setShowSoftInputOnFocus(true);

获得焦点

edittext.requestFocus();

And

alertSearch.setView(edittext);
alertSearch.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Do something
        act.recreate();
    }
});
alertSearch.show();

在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;等等;-)

感谢大家的投票。

问题似乎是,因为你输入文本的地方最初是隐藏的(或嵌套或其他),AlertDialog自动设置标志WindowManager.LayoutParams。FLAG_ALT_FOCUSABLE_IM或WindowManager.LayoutParams。FLAG_NOT_FOCUSABLE,这样就不会触发软输入来显示。

解决这个问题的方法是添加以下内容:

(...)
// Create the dialog and show it
Dialog dialog = builder.create()
dialog.show();

// After show (this is important specially if you have a list, a pager or other view that uses a adapter), clear the flags and set the soft input mode
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

这是一个很好的例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:weightSum="1" >

        <EditText
            android:id="@+id/txtInpuConversation"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:hint="@string/edt_Conversation" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btnSend"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/btn_Conversation" />
    </LinearLayout>

</LinearLayout>