I'm new to Android development and I can't seem to find a good guide on how to use an onKeyUp
listener.
In my app, I have a big EditText
, when someone presses and releases a key in that EditText
I want to call a function that will perform regular expressions in that EditText
.
I don't know how I'd use the onKeyUp. Could someone please show me how?
If you use device with no touch but with hard keypad buttons (keyboard) use this code to control the events of moving left right up down and ok. use onkeyDown and not onKeyUp because the onKeyup will return the next button events:
Just for an
EditText
. its achieve bysetOnClickListener()
itself. No Need for theonKeyUp()
. Because that will perform by a click/Touch Event. right? Just do this.Writing this out of my head but it should be among the following lines:
The very right way is to use TextWatcher class.
You may consider using the following code:
Here In the end we have called the super.onkeyUp method. Which handles the event when the user do not presses the valid key.
For more details you may consider following link.
CORRECTION:
For a while, I used a generic onKeyListener. I soon found that my code was being called twice. Once with the key down and once with key up. I now use the following listener and only call the code once.
"if (event.getAction() == KeyEvent.ACTION_UP)"
is the key.I found that
onKeyUp()
is called automatically for every control in the Activity. If this is what you want, add it to the Activity just like you add theonCreate()
Example:
I know this is a old question, but maybe this will help others with the same issue.