Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Handle "Enter" in an EditText

I am wondering if there is a way to handle the user pressing Enter while typing in an EditText, something like the onSubmit HTML event.

Also wondering if there is a way to manipulate the virtual keyboard in such a way that the "Done" button is labeled something else (for example "Go") and performs a certain action when clicked (again, like onSubmit).

like image 673
Felix Avatar asked Sep 28 '09 23:09

Felix


People also ask

How to handle Enter in EditText android?

myTextView. setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (event. getAction() == KeyEvent. KEYCODE_ENTER) { // Handle pressing "Enter" key here handled = true; } return handled; } });

What is Imeoption Android?

android:imeOptions="actionSend" /> You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND . For example: Kotlin Java.


1 Answers

I am wondering if there is a way to handle the user pressing Enter while typing in an EditText, something like the onSubmit HTML event.

Yes.

Also wondering if there is a way to manipulate the virtual keyboard in such a way that the "Done" button is labeled something else (for example "Go") and performs a certain action when clicked (again, like onSubmit).

Also yes.

You will want to look at the android:imeActionId and android:imeOptions attributes, plus the setOnEditorActionListener() method, all on TextView.

For changing the text of the "Done" button to a custom string, use:

mEditText.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER); 
like image 156
CommonsWare Avatar answered Oct 14 '22 16:10

CommonsWare