Setting EditText to a single line results in loss of focus after pressing enter

I use EditText in my code and compare its contents with a string when a button is clicked. Unfortunately, doing this with the enter key via onKey causes problems because the input creates a line break. I used:

setSingleLine(true); 

to prevent this. But now pressing enter causes the EditText to lose focus. Why does he behave like that and how to fix him?

+6
source share
2 answers

Try using this android:lines="1" in your xml layout for EditText.

This will consider Enter Key as a new line, and the focus will not disappear. Although existing text may not be displayed due to only one line, as it moves the rising word and hides.

+2
source

Set EditText as the following focus:

 <EditText android:id="@+id/input" ... android:imeOptions="actionNext" android:nextFocusForward="@+id/input" android:singleLine="true" /> 
0
source

All Articles