Android 检测括号和特殊字符输入,在 api 11 之前
我有一个屏幕,我希望用户仅在文本字段中输入数字。我已经在 Android 中强制打开数字键盘,并且我正在根据其 ascii 值检测数字。
我正在考虑尝试不使用 KeyCode.NUMBER_PAD_X 来检测数字键盘按键,因为这只是 API 11 及更高版本的功能,并且我寻求与类似功能的更高兼容性。同样,检测特殊字符,如 ( ) + - / # , .是 API 11 及更高版本的内置函数。
在 Android 3.0/API 11 以下的某些 Android 手机上,数字键盘/电话键盘上也有特殊字符,API 根本不会忽略这些字符。在 G2 上,仅忽略一些特殊字符,但它允许将 ( ) + - / # 输入到 EditText 字段中,我的字符捕获器不会注意到这些输入,即使它们超出了该条件的范围,并且我无法使用 API 11 级别的 KeyCodes 显式检测它们
有关如何检测它们的任何解决方案吗?
问题在于,用户可以输入它们并且它们出现在文本字段中,但我的代码都没有检测到它们,也没有在调试器中注册它们。它与我当前检查字符串有多长的其他错误处理条件相混淆,用户可能会用这些字符溢出字符串
I have a screen where I want the user to only input numbers in the text field. I already force a number pad to open in Android, and I am detecting numbers based on their ascii value.
I am deliberating trying not to use KeyCode.NUMBER_PAD_X to detect number pad key presses, because that is only a feature of API 11 and up, and I seek higher compatibility with similar features. Similarly, detecting special characters like ( ) + - / # , . is a built in function of API 11 and up.
On some Android phones below Android 3.0/API 11 , the numeric keypad/phone pad also has special characters on it, that the API simply won't ignore. On the G2, only some special characters are ignored, but it allows for ( ) + - / # to be input into the EditText field, my character catcher does not notice these inputs even though they are outside of the range for that condition, and I can't use the API 11 level KeyCodes to explicitly detect them
Any solutions on how to detect them?
It is problematic that the user can input them and that they appear in the text field, yet none of my code detects them nor do they register in the debugger. It messes with my other error handling conditions which currently check to see how long a string is, the user may be able to overflow the strings with these characters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 EditText 上使用 android:digits 来限制视图将接受的值。
例如:
仅接受数字。键盘仍将包含其他键,但实际上只会接受您允许的键。
You could use android:digits on your EditText to restrict the values that the view will accept.
For example:
Would accept only numbers. The keyboard will still contain other keys, but only the ones you allow will actually be accepted.