开发者

Android: Blocking keys from virtual keyboard

开发者 https://www.devze.com 2023-02-06 16:53 出处:网络
I want to allow the user to enter only certain characters to an EditText on a galaxy tab (Android 2.2) but block all other characters. An OnKeyListene开发者_高级运维r seems not to react to the softwar

I want to allow the user to enter only certain characters to an EditText on a galaxy tab (Android 2.2) but block all other characters. An OnKeyListene开发者_高级运维r seems not to react to the software keyboard.

PS: I dont want to block entire ranges of characters, like all digits or all special chars and so on. Let's say just the character 't'.

Please help. After an hour I couldn't find a solution.


I believe you need the curiously and inconsistently named TextWatcher, and then call EditText.addTextChangedListener() with it.


Call the onKeyDown event, and listen for the key that is pressed, if the key is one that is allowed, add it to your string, if it isnt, dont add it to the string. That, or strip the string after keydown of all invalid characters. Try http://www.rgagnon.com/javadetails/java-0030.html for an answer on that. Thats java code, but I believe all of that carries over into android. Hope that helps

0

精彩评论

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