首页 文章

软键盘不会出现在DialogFragment中的EditText RecyclerView上

提问于
浏览
3

我正在尝试创建一个包含EditTexts的RecyclerView的DialogFragment . 它会滚动,当我单击EditText时会出现复制/剪切/粘贴,但键盘永远不会出现 . 适配器工作,因为我尝试在Activity中实现RecyclerView .

我已经尝试找到使键盘显示的解决方案,例如在XML中添加它

</request focus>

或者这个对话框

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

但仍然没有效果 .

非常感谢 .

附加:这是它目前的出现方式

enter image description here

3 回答

  • 4

    a)强制打开输入法 .

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

    b)要求获得焦点的 EditText 焦点 .

    editText.requestFocusFromTouch();
    
  • 0

    我知道这个问题是在一年前问过的,但今天我问了同样的问题,所以看来这个问题仍然令人困惑......

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

    注意!如果您使用其他方式在 onCreateDialog(Bundle) 中创建对话框,而不是使用 AlertDialog 及其 AlertDialog.Builder ,则此解决方案可能无效 .

    Here is the solution that worked for me.

    通过执行上述解决方案建议,您将"tricking"为您设置正确的标志 . 我是一个糟糕的代码实践,但它可能不是理想的代码实践 . 对于那些不仅寻求快速解决方案但准备深入研究此事的人,请继续阅读:

    因此,根据AlertDialog's documentationAlertDialog 会根据对话框中的任何视图是否从 View.onCheckIsTextEditor() 返回true来自动设置一些窗口放置相关的标志 . AlertLayout 的这种行为由this solution提出,这似乎帮助了很多人提出同样的问题,但没有 RecyclerView 参与,只涉及 AlertDialog ,而不是 DialogFragmentDialogFragment 子类使用 .

    您可能会让后一个解决方案工作(查看其最常投票的评论),但是您的DialogFragment子类必须提供一种方法,在显示 DialogFragment 之后,为了让 AlertDialog 's window, so that you can modify its flags. I haven'尝试这个,因为我的 DialogFragment 没有提供那个功能 .

  • 2

    尝试使用:

    InputMethodManager imm1 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            edt_user_name.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    edt_user_name.requestFocus();
                    imm1.showSoftInput(edt_user_name, 0);
                }
            }, 100);
    

    对于特定的EditText,例如 edt_user_name .

相关问题