首页 > 解决方案 > 浮动标签 TextInputLayout insideTextInputEdittext 添加清除图标右侧角与 onclick 功能

问题描述

我正在尝试在编辑文本框右侧角添加清除图标,并能够使用 Android 中的文本观察器功能清除键入的文本。

同时,我正在尝试在里面使用 Floating label edittext。但是,我无法在TextInputEditText.

注意:我尝试过onDrawableRight使用方法添加图像onTouchListener,有些情况对我来说不适用。

标签: androidmaterial-designandroid-textinputlayoutmaterial-componentsmaterial-components-android

解决方案


只需将TextInputLayout预打包 app:endIconMode="clear_text"属性一起使用。

   <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/custom_end_icon"
        android:hint="Hint text"
        app:endIconMode="clear_text"
        ...>

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ../>

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

在此处输入图像描述


推荐阅读