如何在vewPager中禁用键盘(android)

时间:2015-11-10 15:55:29

标签: android android-edittext android-softkeyboard

我尝试使用这些解决方案在我的片段(其中有我的viewPager)中的onCreateView上隐藏键盘:

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



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



InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(windowToken, 0);


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

});

但所有这些解决方案都不起作用,并且,当editText聚焦时,键盘会出现。

更新: 我尝试在片段的onCreatedView中使用这些代码

mViewPager.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if (mViewPager.getCurrentItem() == 0) {
                final EditText initialValue = (EditText) mViewPager.findViewById(R.id.initialValueEditText);
                final EditText increase = (EditText) mViewPager.findViewById(R.id.increaseEditText);

                initialValue.setOnTouchListener(new View.OnTouchListener() {
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        evalue = "1";
                        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

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

                        InputMethodManager imt = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imt.hideSoftInputFromWindow(initialValue.getWindowToken(), 0);
                        return true;
                    }
                });

                increase.setOnTouchListener(new View.OnTouchListener() {
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        evalue = "2";
                        return false;

                    }
                });
            }
            return true;
        }
    });

view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

            getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    });

但不幸的是,当我关注editText时,会出现键盘

1 个答案:

答案 0 :(得分:0)

在editText上添加android:enabled =“false”。 但是整个editText都不可编辑,这就是你想要的吗?