如何在searchview和edittext上永久隐藏软键盘

时间:2016-01-29 09:53:41

标签: android keyboard

我想在我的应用程序中永久隐藏软键盘。所以我可以使用自定义键盘。我已经检查了很多解决方案,但仍然出现了软键盘。这是我注册searchview的代码。

   EditText mEditText;
    SearchView mSearchView;
    public void registerSearchView(final SearchView sview, final MenuItem searchItem ) {
        mSearchView = sview;
        SearchManager searchManager = (SearchManager) mHostActivity.getSystemService(Context.SEARCH_SERVICE);
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(mHostActivity.getComponentName()));
        mEditText = (EditText) mSearchView.findViewById(R.id.search_src_text);
        ImageView closeButton = (ImageView) mSearchView.findViewById(R.id.search_close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditText.getText() == null || mEditText.getText().length() < 1) {
                    mSearchView.onActionViewCollapsed();
                    MenuItemCompat.collapseActionView(searchItem);
                    hideCustomKeyboard();
                } else {
                    mEditText.setText("");
                    mSearchView.setQuery("", false);
                }
                //Collapse the search widget
            }
        });
        mEditText.setOnTouchListener(onTouch);
        mSearchView.setOnTouchListener(onTouch);
        mEditText.setOnFocusChangeListener(oFocusChange);
        mSearchView.setOnFocusChangeListener(oFocusChange);
        mEditText.setOnClickListener(onClickListener);
        mSearchView.setOnClickListener(onClickListener);
        mSearchView.setOnSearchClickListener(onClickListener);


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
            showCustomKeyboard(v);
            v.clearFocus();
            v.requestFocus();

        }
    };
    private View.OnTouchListener onTouch = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {
            if (mEditText.getText()==null || mEditText.getText().length() < 1){
                v.clearFocus();
                v.requestFocus();
            }
            ((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
            v.onTouchEvent(motionEvent);
            return true;
        }
    };
    private View.OnFocusChangeListener  oFocusChange = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean b) {
            if (b == false) {
                hideCustomKeyboard();
            } else {
                ((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
                showCustomKeyboard(v);
            }
        }
    };

2 个答案:

答案 0 :(得分:1)

这会起作用,因为你可以强制android隐藏键盘!!

public static void disableSoftInputFromAppearing(EditText editText) {
    if (Build.VERSION.SDK_INT >= 11) {
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);
    } else {
        editText.setRawInputType(InputType.TYPE_NULL);
        editText.setFocusable(true);
    }
}

上面的代码将起到预防作用!!

以及搜索视图

 mSearchView.clearFocus();

将解决问题

答案 1 :(得分:0)

将此添加到您的清单:

 <activity android:name="com.your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />