软键盘未显示在DialogFragment的EditText RecyclerView上

Yeo*_*eol 4 android android-softkeyboard android-edittext android-recyclerview

我试图创建一个包含EditTexts的RecyclerView的DialogFragment。当我单击EditText时,它会滚动并显示“复制/剪切/粘贴”,但键盘永远不会出现。自从我尝试在Activity中实现RecyclerView以来,适配器就起作用了。

我已经尝试过寻找使键盘显示出来的解决方案,例如将其添加到XML中。

</request focus>
Run Code Online (Sandbox Code Playgroud)

还是这个到对话框

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

但仍然没有任何效果。

非常感谢。

其他:这是目前的样子

在此处输入图片说明

小智 7

我知道这个问题是一年前提出的,但是今天我问了同样的问题,所以这个问题似乎仍然令人困惑。

无论如何,在浏览Stack Overflow和Android API参考之后,我遇到了多个“解决方案”,但是只有一个可行。现在,我不确定为什么这些其他看似好的解决方案对我不起作用。但是至少我认为我理解(​​或有理论)为什么最终有效的解决方案有效。

注意!如果您使用其他方法而onCreateDialog(Bundle)不是利用AlertDialog及其对话框创建对话框,则此解决方案可能不起作用AlertDialog.Builder

这是对我有用的解决方案。

通过上述解决方案建议,您正在“努力” AlertDialog为您设置正确的标志。我并不是说这是不好的代码实践,但它可能不是理想的代码实践。对于那些不只是寻求快速解决方案而且准备深入研究此问题的人,请继续阅读:

因此,根据AlertDialog的文档AlertDialog根据对话框中是否有任何视图从返回true来自动设置一些与窗口放置相关的标志View.onCheckIsTextEditor()。这种行为AlertLayout被提出了这一解决方案,似乎已经帮助了很多人问同样的问题,但没有RecyclerView涉及且仅AlertDialog涉及,而不是AlertDialog使用一个DialogFragment子类。

您可能使后一种解决方案有效(请查看其最受好评的评论),但是DialogFragment子类必须提供一种在显示之后的方法DialogFragment,以获取AlertDialog的窗口,以便您可以修改其标志。我没有尝试过,因为我DialogFragment没有提供该功能。


Fel*_*ira 7

在为扩展 a 的类创建的aRecyclerView.Adapter中使用 , 时,我遇到了同样的问题。另外,在我的程序中,我使用的键盘在获得焦点时不会显示键盘。viewDialogFragmentViewHolderEditText

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_text_detected_list, null);
    //{...setAdapter, and other setups...}
    AlertDialog dialog = builder.setView(view).create();

    //The below line, solves my problem with the keyboard not showing up
    dialog.setOnShowListener(d -> dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM));
    return dialog;
}
Run Code Online (Sandbox Code Playgroud)


Rya*_*yan 5

a) 强制打开输入法。

InputMethodManager inputMananger = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMananger.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud)

b) 请求EditText您希望获得焦点的焦点。

editText.requestFocusFromTouch();
Run Code Online (Sandbox Code Playgroud)

  • 你好!方法 a 确实使键盘出现。但是,它显示在对话框片段下方。我该如何解决这个问题? (3认同)