我已经在EditText小部件中添加了文本右侧的图像,使用以下XML:
<EditText
android:id="@+id/txtsearch"
...
android:layout_gravity="center_vertical"
android:background="@layout/shape"
android:hint="Enter place,city,state"
android:drawableRight="@drawable/cross" />
但我想在单击嵌入图像时清除EditText。我该怎么做呢?
还有更优雅的方式:
不要在TextInputEditText中使用android:drawableRight或android:drawableEnd。
相反,你可以用:
<com.google.android.material.textfield.TextInputLayout
...
app:endIconMode="custom"
app:endIconDrawable="@drawable/..."
然后使用endIconOnClickListener:
textInputLayout.setEndIconOnClickListener {
// Respond to end icon presses
}
来源:https://stackoverflow.com/a/65940540/13545849