Get the key pressed in EditText

I want to know which key was pressed in EditText . For example, if you press a , I want to get the value as 'a'. How can i do this?

+4
source share
2 answers

You can set onKeyListener() to EditText and get a KeyCode this way. For instance:

 editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { switch(keyCode) { case KeyEvent.KEYCODE_0: //handle code for pressing 0 break; default: break; } } }); 

And in your switch just process all the necessary key codes. A complete list can be found in KeyEvent constants.

EDIT: Keep in mind for verification and the like, TextWatcher, as mentioned by Nicholas, may be the preferred solution if you need to know that the CHARACTER has been entered (for example, “A” versus “a”, how you will have to process the logic of whether the shift key is active or not in the key listener). If you just need to know which key was pressed, I would recommend OnKeyListener.

+5
source

Take a look at my answer here:

Android edittext check

0
source

All Articles