How do I hide the soft keyboard when changing tabs?

后端 未结 5 1345
耶瑟儿~
耶瑟儿~ 2021-01-05 13:06

EDIT : Seems I\'m not making myself clear. What I need is a way to hide the soft keyboard whenever i replace the fragment I am in. How do I go about doing this ?

5条回答
  •  -上瘾入骨i
    2021-01-05 13:50

    programmatically you could use, capturing the view of the active activity on the screen of the device.

    public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
            View focus = getCurrentFocus();
            if (focus != null) {
                hiddenKeyboard(focus);
            }
        }
    public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
            View focus = getCurrentFocus();
            if (focus != null) {
                hiddenKeyboard(focus);
            }
        }
    public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
            View focus = getCurrentFocus();
            if (focus != null) {
                hiddenKeyboard(focus);
            }
        }
    
    private void hiddenKeyboard(View v) {
            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    

提交回复
热议问题