通常我们在手机上使用输入框的时候,希望点击飞输入框区域,然后就能自动关闭输入框。
那么我们只需要在 Activity 中获取到点击事件,然后执行一个关闭输入框的操作就行。
实现代码如下:
1.在 Activity 类中重写 onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
closeKeyBoard();
return super.onTouchEvent(event);
}
public void closeKeyBoard() {
if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
View v = getCurrentFocus();
ScreenUtil.closeSoftInput(this, v);
}
}
2.关闭软键盘
// 关闭键盘输入法
public static void closeSoftInput(Context context, View v) {
if (v != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}