1
0

More than 1 year has passed since last update.

【Android】EditTextでエンター押下後フォーカスがループしない

Last updated at Posted at 2022-08-31

不具合の事象

2個のEditTextを用意し、それぞれandroid:imeOptions="actionNext"を付与。
2個目のEditText内でエンターを押した場合、1個目のEditTextにフォーカスがループするときとしない時がある。

ループする ループしない
normal.gif false.gif

原因

画面内に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>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0