Programmatically Hide Soft Keyboard in ViewPager.OnPageChangeListener onPageSelected()?

后端 未结 5 945
醉酒成梦
醉酒成梦 2021-02-02 09:39

I have a ViewPager + ActionBar with tabs. I want to make the soft keyboard hide when I \"swipe\" to another tab but I can\'t figure out how.

I\'ve passed in my Activity

5条回答
  •  不知归路
    2021-02-02 10:41

    In your activity, you can do the following:

    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            final InputMethodManager imm = (InputMethodManager)getSystemService(
                Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
        }
    
        @Override
        public void onPageScrolled(int position, float offset, int offsetPixels) {
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    

提交回复
热议问题