找到布局最外面的控件,然后自定义一个这个控件,重写里面的一个方法,然后从外面传入一个Activity
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (context != null) {
boolean isKey = isSoftShowing(context);
if(isKey){
hideSoftInput(context);
return true;
}
}
return super.dispatchTouchEvent(ev);
}
private boolean isSoftShowing(Activity context) {
//获取当前屏幕内容的高度
int screenHeight = context.getWindow().getDecorView().getHeight();
//获取View可见区域的bottom
Rect rect = new Rect();
context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return screenHeight - rect.bottom != 0;
}
public void hideSoftInput(final Activity activity) {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm == null) return;
View view = activity.getCurrentFocus();
if (view == null) view = new View(activity);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}