开发者

Android detect parenthesis and special character input, before api 11

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 belo开发者_StackOverfloww 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


You could use android:digits on your EditText to restrict the values that the view will accept. For example:

android:digits="0123456789"

Would accept only numbers. The keyboard will still contain other keys, but only the ones you allow will actually be accepted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜