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

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

    In my case for fragment activity following is working fine.

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

    UPDATE: This was my fragment activity from which two fragments(CommentFragment,AnotherFragment) are opening in a ViewPager

    public class CommentActivity extends FragmentActivity {
        ViewPager pager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            pager = (ViewPager) findViewById(R.id.viewPager);
            .......
            .......
        }
    
        private class MyPagerAdapter extends FragmentPagerAdapter {
    
            public MyPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int pos) {
                switch(pos) {
                    case 0: return CommentFragment.newInstance();
                    default: return AnotherFragment.newInstance();
                }
            }
    
            @Override
            public int getCount() {
                return 2;
            }       
        }
    
    }
    

    If keypad is appear for an EditText in CommentFragment and want to switch to AnotherFragment and want to hide keypad at the time of switch

    public class CommentFragment extends Fragment  {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            .........
            .........
            .........
            ((CommentActivity)getActivity()).pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
             @Override
             public void onPageSelected(int position) {
                final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(txtComment.getWindowToken(), 0);
             }
    
             @Override
             public void onPageScrolled(int position, float offset, int offsetPixels) {
             }
    
             @Override
             public void onPageScrollStateChanged(int state) {
             }
       });
    }
    
    0 讨论(0)
  • 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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 10:30

    Simple way :

    InputMethodManager imm = (InputMethodManager)    
    getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    
    0 讨论(0)
  • 2021-02-02 10:36

    Common way:

    private void hideVirturalKeyboard(){
    View v = getCurrentFocus();
    if (v != null && v instanceof EditText) {
    InputMethodManager mgr = (InputMethodManager)(v.getContext().getSystemService(Context.INPUT_METHOD_SERVIC
    E));
    mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    } 
    
    0 讨论(0)
  • 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) {
        }
    });
    
    0 讨论(0)
提交回复
热议问题