可能大家都遇到多个输入框,当某个输入框有焦点时,光标显示。当我们点击输入框以外的地方。使输入框焦点消失,隐藏键 盘。
//使editText点击外部时候失去焦点
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {//点击editText控件外部
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
assert v != null;
KeyBoardUtil.closeKeyboard(v);//软键盘工具类
if (editText != null) {
editText.clearFocus();
}
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}
EditText editText = null;
public boolea
如果觉得《android edittext失去焦点 Android 点击EditText外部区域失去焦点的方法》对你有帮助,请点赞、收藏,并留下你的观点哦!