首页 > 代码库 > 强制隐藏输入框软键盘

强制隐藏输入框软键盘

 

   //把下列代码放入BaseActivity中即可

/**
* 判断是否需要隐藏软键盘
* @param v
* @param event
* @return
*/
private boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] l = {0, 0};
v.getLocationInWindow(l);
int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
+ v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {

return false;
} else {
return true;
}
}
return false;
}

/**
* 隐藏软键盘
*
* @param token
*/
private void hideSoftInput(IBinder token) {
if (token != null) {
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(token,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}

强制隐藏输入框软键盘