如何针对特定活动禁用 Android 软键盘?
我有一个包含一个 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 字段中输入字母和其他符号,而我希望每个人都能具体而言,仅使用键盘在字段中输入数据。
我还应该提到,我目前正在 android 2.1 下测试我的应用程序,但我希望我的解决方案适用于所有版本。任何帮助将不胜感激。提前致谢。
I have an activity with one EditText where I need to input numbers only.
Now, I have defined the Input Type for my EditText to be number only and have drawn up a pretty keypad for my user to use, however I also need to make sure the soft keyboard doesn't pop up for my user when they click on the EditText.
I have tried hiding the keyboard through the manifest by adding
android:windowSoftInputMode="stateAlwaysHidden"
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您需要禁用软键盘的任何 Edittext 中,在该编辑文本的 xml 中添加属性。
它将停止软键盘的执行。
In Whichever Edittext you need to disable the soft keyboard, add attribute in xml of that edit text.
It will stop the execution of soft keyboard.
你可以试试这个。它对我有用:
You can try this. It is working for me:
在清单文件的 Activity 中使用
android:windowSoftInputMode="stateHidden"
。这会起作用。不要对 EditText 使用 android:focusable="false" ,除非您不愿意输入文本。如果您想提供输入,请删除布局文件中 edittext 的该属性。Use the
android:windowSoftInputMode="stateHidden"
in your activity in manifest file. This will work. Don't use the android:focusable="false" for EditText unless if you are not willing to input the text. If you want to give input then remove that property for the edittext in the layout file.例如,如果您使用 ViewGroup,它允许您使用此方法阻止软键盘:
因此,视图将在其任何后代之前获得焦点。
If you are using for example ViewGroup it allows you to block the soft keyboard using this method:
Thanks to this the view will get focus before any of its descendants.
您可以使用此代码:
you can use This Code:
只是认为可能可以扩展 EditText 并通过那里处理对软键盘的访问,并遇到 这个问题对我的问题有一个非常优雅的解决方案。
只需按照步骤操作即可,这对我来说完美地处理了软键盘。希望它可以帮助遇到此问题的其他人。
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.