300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android edittext失去焦点 Android 点击EditText外部区域失去焦点的方法

android edittext失去焦点 Android 点击EditText外部区域失去焦点的方法

时间:2023-08-19 10:14:36

相关推荐

android edittext失去焦点 Android 点击EditText外部区域失去焦点的方法

可能大家都遇到多个输入框,当某个输入框有焦点时,光标显示。当我们点击输入框以外的地方。使输入框焦点消失,隐藏键 盘。

//使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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。