简体   繁体   中英

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.

On Android 4.2.2 it works fine, on 2.3.x it does not, neither in emulator nor on real device. On the emulator I can type with my windows keyboard although the soft keyboard is not showing.

I have not hidden the keyboard on purpose. Showing the keyboard with following code only works for 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:

@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.

This still does not work on the 2.3 emulator, but it works on the real device. Seems to be some kind of 2.3 bug.

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. The solution of Gunnar Bernstein did work, but I wanted the EditText to be without focus initially. Based on some other answers on SO I came up with the following solution:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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