別のアイテムをタップした時に特定のEditTextにフォーカスする方法です。
何らかの理由で、EditTextの大きさを稼げないときに使用します。下のコードは、EditTextが小さくても外側の要素をタップすることでフォーカスさせられる処理です。
android:hint
に何か文字を入れないと、うまく処理されなかったです。下記コードは半角スペースを入れています。
container.setOnClickListener {
editText.requestFocus()
val inputMethodManager: InputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(editText, 0)
}
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" "
android:inputType="text"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
参考