Android Studio:EditText で setOnClickListener を発火させたい

とりあえず二パターン

1.xml と Kotlin を使う方法

xml

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autofillHints=""
    android:inputType="textPersonName"
    android:clickable="true"
    android:focusable="false"/>

Kotlin

val editText:EditText = findViewById<>(R.id.editText)
editText.setOnClickListener(){
    //do something
}

2.Kotlinのみの方法

import android.view.View.NOT_FOCUSABLE
val editText:EditText = findViewById<>(R.id.editText)
editText.focusable = NOT_FOCUSABLE
editText.clickable = true
editText.setOnClickListener(){
//do something
}

import があるので注意

おわりに

詳しく知りたい方はこのあたり
https://developer.android.com/reference/android/view/View#attr_android:clickable
https://developer.android.com/reference/android/view/View#setClickable(boolean)
https://developer.android.com/reference/android/view/View#FOCUSABLE
https://developer.android.com/reference/android/view/View#setFocusable(int)

てか focusable が Kotlin だと Int なのに xml だと boolean ?.....
まあAUTO があるせいか

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA