Вопрос

I use an EditText in my code and compare its content to a string when clicking on a button. Unfortunately doing this with the enter key through onKey causes problems because enter creates a line break. I used:

setSingleLine(true);

to prevent this. But now pressing enter leads to the EditText losing its focus. Why does it behave like this and how do I fix it?

Это было полезно?

Решение

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

This will consider the Enter Key as new line and focus will not loose. Although existing text might not be visible due to only one line, as it moved upword and get hidden.

Другие советы

Set the EditText as the next focus of itself:

<EditText
    android:id="@+id/input"
    ...
    android:imeOptions="actionNext"
    android:nextFocusForward="@+id/input"
    android:singleLine="true" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top