Softkeyboard in Fragment does not show for edittex

2019-08-11 10:32发布

问题:

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?

回答1:

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.