首页 > 解决方案 > 设置点击监听器到 TextInputEditText drawable Right/End android studio

问题描述

我有一个文本输入布局,里面有文本输入编辑文本。文本输入编辑文本最后有一个 Drawable。我想要实现的是让drawable在最后被点击时做一些事情,例如显示一个Toast消息

下面是我的 XML 代码


<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til_toast"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:padding="3dp">

            <!--android:maxLength="13"-->
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et_toast"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableEnd="@drawable/ic_calendar"
                android:drawableRight="@drawable/ic_calendar"
                android:hint="@string/date_from"
                android:inputType="date" />

        </com.google.android.material.textfield.TextInputLayout>

我怎样才能做到这一点

标签: androidandroid-edittextandroid-textinputlayoutandroid-textinputedittext

解决方案


不要在. android:drawableRight_android:drawableEndTextInputEditText

相反,您可以使用:

<com.google.android.material.textfield.TextInputLayout
    ...
    app:endIconMode="custom"
    app:endIconDrawable="@drawable/..."

然后使用endIconOnClickListener

textInputLayout.setEndIconOnClickListener {
  // Respond to end icon presses
}

推荐阅读