Android点击空白处隐藏输入框
16lz
2021-01-23
通常我们在手机上使用输入框的时候,希望点击飞输入框区域,然后就能自动关闭输入框。
那么我们只需要在 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); }}
更多相关文章
- Android软键盘弹出时把布局顶上去的解决方法
- android初始化activity时隐藏软键盘
- 【Android】代码实现模拟屏幕点击和键盘按键事件
- 安卓布局被软键盘顶上去的解决方案
- Android中软键盘弹出时关于布局的问题