以编程方式在ViewPager.OnPageChangeListener onPageSelected()中隐藏软键盘?

时间:2012-08-05 18:20:42

标签: android android-fragments android-viewpager android-softkeyboard

我有一个带标签的ViewPager + ActionBar。当我“滑动”到另一个标签时,我想隐藏软键盘,但我无法弄清楚如何。

我已经将我的Activity传递给了FragmentPageAdapter的构造函数,所以我可以调用

 activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

但它没有做任何事情(而且它在代码的可到达区域内)......帮助?

5 个答案:

答案 0 :(得分:41)

在您的活动中,您可以执行以下操作:

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) {
    }
});

答案 1 :(得分:31)

我不使用onPageSelected(),因为隐藏键盘动画会带有滑动动画。而是使用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);
        }
    }
}

答案 2 :(得分:5)

常用方式:

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);
}
} 

答案 3 :(得分:1)

简单方法:

InputMethodManager imm = (InputMethodManager)    
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

答案 4 :(得分:1)

在我的情况下片段活动以下工作正常。

((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) {
     }
});

<强>更新 这是我的片段活动,其中两个片段(CommentFragment,AnotherFragment)在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;
        }       
    }

}

如果在CommentFragment中为EditText显示小键盘,并且想要切换到AnotherFragment并希望在切换时隐藏小键盘

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) {
         }
   });
}