StanE
StanE

Reputation: 2783

Android: Blocking keys from virtual keyboard

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 OnKeyListener 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.

Upvotes: 0

Views: 1279

Answers (2)

Shaun
Shaun

Reputation: 5531

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

Upvotes: 0

Reuben Scratton
Reuben Scratton

Reputation: 38727

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

Upvotes: 1

Related Questions