How to hide the soft keyboard from inside a fragment?

前端 未结 14 1641
予麋鹿
予麋鹿 2020-12-01 00:08

I have a FragmentActivity using a ViewPager to serve several fragments. Each is a ListFragment with the following layout:



        
14条回答
  •  萌比男神i
    2020-12-01 00:15

    As long as your Fragment creates a View, you can use the IBinder (window token) from that view after it has been attached. For example, you can override onActivityCreated in your Fragment:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
    }
    

提交回复
热议问题