1. 程式人生 > >強制隱藏輸入框軟鍵盤

強制隱藏輸入框軟鍵盤

pre ram location eve ould eight param else ldh

//把下列代碼放入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);
}
}

強制隱藏輸入框軟鍵盤