如何为特定活动禁用Android软键盘? [英] How to disable Android Soft Keyboard for a particular activity?

查看:94
本文介绍了如何为特定活动禁用Android软键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有EditText的活动,我只需要输入数字即可.

现在,我已将EditText的输入类型定义为仅数字,并绘制了一个漂亮的键盘供用户使用,但是,我还需要确保在以下情况下不会为用户弹出软键盘:他们单击EditText.

我尝试通过添加清单来隐藏清单中的键盘

android:windowSoftInputMode="stateAlwaysHidden"

在我的清单中针对特定活动,但这对我不起作用,因为一旦用户单击EditText,键盘就会再次出现.

我已经尝试过像这样通过编程方式做同样的事情

activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

但这也不起作用.当用户单击EditText时出现键盘.

唯一可行的方法是将EditText的InputType设置为null,如下所示:

EditText.setInputType(InputType.TYPE_NULL);

但是我不能使用它,因为它允许连接了设备的键盘的用户在EditText字段中输入字母和其他符号,而我希望每个人都专门使用小键盘在该字段中输入数据. /p>

我还应该提到,我目前正在android 2.1下测试我的应用程序,但是我希望我的解决方案可以在所有版本上使用.任何帮助,将不胜感激.预先感谢.

解决方案

只是认为可以扩展EditText并通过该处处理对软键盘的访问,并且遇到了

in my Manifest for the particular activity, but this doesn't work for me because as soon as the user clicks on the EditText the keyboard appears again.

I've tried doing the same programmatically like so

activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

but that doesn't work either. Keyboard appears when the user clicks on the EditText.

The only thing that worked was setting InputType to null for the EditText like so:

EditText.setInputType(InputType.TYPE_NULL);

but I cannot use this because it will allow users who have a keyboard attached to their device to input letters and other symbols in the EditText field, while I want everyone to specifically use only the keypad to enter data in the field.

I should also mention that I am currently testing my app under android 2.1, but I would like my solution to work across all versions. Any help would be appreciated. Thanks in advance.

解决方案

Just thought it might be possible to extend EditText and handle the access to the softkeyboard through there and came across this question which has a very elegant solution to my problem.

Just followed the steps and this handled the softkeyboard perfectly for me. Hope it helps others that come across this issue.

这篇关于如何为特定活动禁用Android软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆