Android编程实现点击EditText之外的控件隐藏软键盘功能
作者:_iorilan 时间:2022-08-07 02:06:08
本文实例讲述了Android编程实现点击EditText之外的控件隐藏软键盘功能。分享给大家供大家参考,具体如下:
工具类
...
public static void hideKeyboard(Context ctx) {
if (ctx != null) {
View view = ((Activity) ctx).getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
点击除EDITTEXT之外的控件隐藏软键盘,如果是viewgroup控件,递归执行
public static void setupUI(View view, final Context ctx) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard(ctx);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, ctx);
}
}
}
...
}
调用时只需要传递最外层的layout即可。
UtilApp.setupUI((RelativeLayout) findViewById(R.id.login_parent), mContext);
希望本文所述对大家Android程序设计有所帮助。
标签:Android,控件,软键盘
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java编程Webservice指定超时时间代码详解
2023-11-02 23:17:12
Java中jstat命令的使用详解
2023-12-10 21:50:23
![](https://img.aspxhome.com/file/2023/0/73090_0s.png)
java实现将域名解析成ip示例
2021-11-20 10:33:33
Servlet实现文件的上传与下载
2023-08-08 06:13:43
![](https://img.aspxhome.com/file/2023/2/58152_0s.jpg)
c#代码生成URL地址的示例
2022-02-17 09:44:08
java线程池的四种创建方式详细分析
2022-10-20 22:32:41
![](https://img.aspxhome.com/file/2023/4/69674_0s.png)
C# Winform使用扩展方法实现自定义富文本框(RichTextBox)字体颜色
2021-06-23 15:05:50
Java7到Java17之Switch语句进化史示例详解
2021-11-03 18:47:37
Java数组索引异常产生及解决方案
2023-11-05 16:52:27
![](https://img.aspxhome.com/file/2023/2/58702_0s.png)
Apache Commons Math3探索之多项式曲线拟合实现代码
2023-11-16 22:01:56
java实现多文件上传至本地服务器功能
2022-01-26 11:55:24
如何使用MybatisPlus快速进行增删改查详解
2023-11-03 06:58:13
![](https://img.aspxhome.com/file/2023/1/58741_0s.png)
Android token过期刷新处理的方法示例
2023-11-23 14:11:40
泛谈Java中的不可变数据结构
2022-02-18 00:12:54
unity shader实现玻璃折射效果
2022-04-16 08:22:36
![](https://img.aspxhome.com/file/2023/6/77226_0s.gif)
学习Winform文本类控件(Label、Button、TextBox)
2022-12-29 09:00:48
![](https://img.aspxhome.com/file/2023/5/86985_0s.png)
Java流程控制语句最全汇总(上篇)
2023-11-03 01:57:48
![](https://img.aspxhome.com/file/2023/0/58850_0s.jpg)
OpenCV实现人脸识别简单程序
2023-07-07 00:31:12
Java Synchronized锁的使用详解
2022-10-11 12:59:33
Java如何在PDF中添加ToolTip工具提示
2021-12-31 12:12:53
![](https://img.aspxhome.com/file/2023/6/77456_0s.png)