//在你的Activity类中如下代码
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// 获取当前拥有焦点的视图
View view = getCurrentFocus();
// 检查当前焦点是否在EditText上,并且触摸事件为抬起或移动,同时排除了WebView的EditText
if (view != null && (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_MOVE)
&& view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) {
// 用于存储视图在屏幕上的位置
int[] sourceCoordinates = new int[2];
// 获取视图在屏幕上的位置
view.getLocationOnScreen(sourceCoordinates);
// 计算触摸点相对于视图的坐标,考虑了视图的偏移量
float x = event.getRawX() + view.getLeft() - sourceCoordinates[0];
float y = event.getRawY() + view.getTop() - sourceCoordinates[1];
// 判断触摸点是否落在视图外
if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom()) {
// 获取输入法管理器
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏软键盘
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
// 调用父类的dispatchTouchEvent方法继续分发触摸事件
return super.dispatchTouchEvent(event);
}
06-30
728
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-24
435
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)