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

后端 未结 5 960
醉酒成梦
醉酒成梦 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:27

    I don't use onPageSelected() because the hide-keyboard animation screws with the swiping animation. Instead use onPageScrollStateChanged():

    @Override
    public void onPageScrollStateChanged(int state)
    {
        if (state == ViewPager.SCROLL_STATE_IDLE)
        {
            if (mViewPager.getCurrentItem() == 0)
            {
                // Hide the keyboard.
                ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
            }
        }
    }
    

提交回复
热议问题