How do I hide the soft keyboard when changing tabs?

后端 未结 5 1344
耶瑟儿~
耶瑟儿~ 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条回答
  •  孤城傲影
    2021-01-05 13:50

    I had the same issue and placed the following code in my tab fragment just prior to using the FragmentTransaction.replace() method to change tabs. The onCreate and onCreateView methods in each fragment are not triggered after the initial tab selection, so hiding the keyboard can be done before getting to the specific fragment's class. Using mTabHost.getApplicationWindowToken() rather than editText.getWindowToken() was a major help. Thanks to whoever found that. Sorry I lost the link.

    InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);             
    im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);
    
    .......
    fm = getFragmentManager();
    ....
    
    fm.beginTransaction()
        .replace(placeholder, new someFragment(), tabId)
        .commit();
    

提交回复
热议问题