How to hide the soft keyboard from inside a fragment?

前端 未结 14 1665
予麋鹿
予麋鹿 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条回答
  •  天涯浪人
    2020-12-01 00:34

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_my, container,
                    false);
            someClass.onCreate(rootView);
            return rootView;
        }
    

    Keep an instance of my root view in my class

    View view;
    
    public void onCreate(View rootView) {
        view = rootView;
    

    Use the view to hide the keyboard

     public void removePhoneKeypad() {
        InputMethodManager inputManager = (InputMethodManager) view
                .getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
    
        IBinder binder = view.getWindowToken();
        inputManager.hideSoftInputFromWindow(binder,
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
    

提交回复
热议问题