不具合の事象
2個のEditTextを用意し、それぞれandroid:imeOptions="actionNext"
を付与。
2個目のEditText内でエンターを押した場合、1個目のEditTextにフォーカスがループするときとしない時がある。
ループする | ループしない |
---|---|
原因
画面内にRecyclerView
があるとそれにフォーカスされるので、フォーカスがループされない。Button
などの他のビューではフォーカスされず、RecyclerView
のみフォーカスされた。
対策
nextFocusForward
でフォーカスの移動先のビューを指定すればよい。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text"
android:nextFocusForward="@id/edit_text_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="@+id/edit_text_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text"
android:nextFocusForward="@id/edit_text_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text_1"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text_2"/>
</androidx.constraintlayout.widget.ConstraintLayout>