0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EditTextにフォーカスさせる

Posted at

別のアイテムをタップした時に特定の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>

参考

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?