Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get back key event on EditText

How can I handle the event of pressing back key while typing on an EditText? When the virtual keyboard is shown and the user presses back, it gets hidden. I want to handle this event, but setting an OnKeyListener in the EditText does not help.

like image 264
Flávio Faria Avatar asked Feb 25 '11 03:02

Flávio Faria


1 Answers

Thank you Reno. It probably seems to work, but I managed to solve it differently.

I overrode EditText's onKeyPreIme(int keyCode, KeyEvent event). This method intercepts keypresses on the IME. =D

public boolean onKeyPreIme(int keyCode, KeyEvent event) {     if (keyCode == KeyEvent.KEYCODE_BACK &&          event.getAction() == KeyEvent.ACTION_UP) {             // do your stuff             return false;     }     return super.dispatchKeyEvent(event); } 
like image 113
Flávio Faria Avatar answered Oct 08 '22 17:10

Flávio Faria