android: onKeyListener no se activa para la tecla ENTER presionada para EditText multilínea
Frecuentes
Visto 1,592 veces
0
I've found a lot of similar questions but all of them don't work for multilines EditText.
If i set inputType="text"
EditText becomes singleline and returns are just not shown. I also need "Enter" button to be "Enter" button (not "Next" or "Done").
content.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
keyCode == KeyEvent.KEYCODE_ENTER)
doSmthSpecial();
return false;
}
});
2 Respuestas
2
I don't know why it is not invoking ENTER key event. but you can go for another method which serves you same purpose. use OnEditorActionListener.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doSomethingSpecial();
return true;
}
});
this method is invoked when you press ENTER key on EditText.
respondido 27 nov., 13:06
0
i have also digged alot of time on internet for this question's solution then,i found this:
et.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
et is you edit text.
this,worked like charm for me.
respondido 27 nov., 13:06
does not work for me unfortunately. Does inputType makes sense? - 4ntoine
no,it works for me and i don't this that input type effect's that much, because i have use inputtype as android:inputType="none" - Hamad
oh, i catched it. dispatchKeyEvent() simulates user Enter press, but when the user presses Enter type the event is still not fired. I need to make onKeyListener fired when the user presses Enter. - 4ntoine
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android user-interface or haz tu propia pregunta.
for that, observe the text entered in EditText by using TextWatcher (desarrollador.android.com/reference/android/text/… and search for the character '\n' in text. - Gopal Gopi
este does not work for multilines EditText though does work for singleline (just checked for it) - 4ntoine
for multilined EditText the listener is not fired at all on Enter press - 4ntoine
you must be joking) sure i removed onKeyListener - 4ntoine
nos deja continuar esta discusión en el chat - Gopal Gopi