简体   繁体   English

片段中的软键盘不显示edittext

[英]Softkeyboard in Fragment does not show for edittext

I have a simple application with two fragments. 我有一个带有两个片段的简单应用程序。 The right fragment is being replaced. 正确的片段将被替换。 An edittext inside has requestfocus, but does not show the keyboard. 内部的edittext具有requestfocus,但不显示键盘。

On Android 4.2.2 it works fine, on 2.3.x it does not, neither in emulator nor on real device. 在Android 4.2.2上,它可以正常运行,而在2.3.x上,则不能运行,无论是在模拟器上还是在真实设备上。 On the emulator I can type with my windows keyboard although the soft keyboard is not showing. 在仿真器上,尽管软键盘没有显示,但是我仍可以使用Windows键盘键入内容。

I have not hidden the keyboard on purpose. 我还没有故意隐藏键盘。 Showing the keyboard with following code only works for 4.2.x. 使用以下代码显示键盘仅适用于4.2.x。

InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Any hint? 有什么提示吗?

Doing more research I have found the following. 做更多的研究,我发现了以下内容。

Since I need the focus on the edittext, I first remove the focus and then put it back: 由于我需要将焦点放在edittext上,因此我首先将焦点移开,然后放回原处:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myFilter.clearFocus();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    myFilter.requestFocus();
}

The keyboard is not shown, but once the user clicks on the edittext, it comes up. 键盘没有显示,但是一旦用户单击Edittext,它就会出现。

This still does not work on the 2.3 emulator, but it works on the real device. 这在2.3仿真器上仍然不起作用,但是在实际设备上起作用。 Seems to be some kind of 2.3 bug. 似乎是某种2.3错误。

This seems to be some kind of evergreen problem. 这似乎是一种常绿的问题。 My app just had it on an Android Pie device whereas the problem did not appear on an Android L device. 我的应用仅在Android Pie设备上安装了该应用,而在Android L设备上未出现该问题。 The solution of Gunnar Bernstein did work, but I wanted the EditText to be without focus initially. Gunnar Bernstein的解决方案确实可行,但我希望EditText最初没有重点。 Based on some other answers on SO I came up with the following solution: 基于SO的其他答案,我提出了以下解决方案:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        CoroutineScope(Dispatchers.Main).launch {
            delay(100)
            my_edit_view.clearFocus()
        }
    }

Hope this helps someone else as well. 希望这对其他人也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM