Android隐藏软键盘

1. 隐藏软键盘的方法,代码如下

InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);


2. 点击空白位置自动隐藏软键盘

需要重写onTouchEcent()

代码如下:

@Override public boolean onTouchEvent(MotionEvent event) {
    if(null != this.getCurrentFocus()){
      InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
      return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }
    return super .onTouchEvent(event);
  }

猜你喜欢

转载自blog.csdn.net/weihua_li/article/details/72954132