2020-03-31
首先用了 这个第三方工具库: api 'com.blankj:utilcode:1.13.11'
然后在Activity页面里:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideKeyboard(v, ev)) {
KeyboardUtils.hideSoftInput(this);
if (KeyboardUtils.isSoftInputVisible(SearchActivity.this)) {
return true;
}
}
}
return super.dispatchTouchEvent(ev);
}
// Return whether touch the view.
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
if ((v instanceof EditText)) {
int[] l = {0, 0};
v.getLocationOnScreen(l);
int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
return !(event.getRawX() > left && event.getRawX() < right && event.getRawY() > top && event.getRawY() < bottom);
}
return false;
}
网友评论