TextInputLayout不会设置errorIconDrawable

问题描述

当我将style="@style/Widget.Design.TextInputLayout"用于TextInputLayout时,设置app:errorIconDrawable不会执行任何操作。仅当我未设置样式并让它继承应用程序主题Theme.MaterialComponents.Light.NoActionBar)时,它才起作用。 app:endIconDrawable也不起作用,我找不到该问题的替代方案/解决方案。请帮忙!


以下在继承应用程序主题时起作用:Theme.MaterialComponents.Light.NoActionBar,但我不希望这种样式,特别是下划线与文本不符:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

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

enter image description here


这使用了我需要的样式,但不会显示错误图标:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    style="@style/Widget.Design.TextInputLayout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

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

enter image description here

解决方法

由于您使用的是旧式Widget.Design.TextInputLayout,因此它不支持*.OutlinedBox*.FilledBox样式具有的所有文本字段功能。

您无法在xml中设置errorIconDrawable,必须在TextInputLayout.setErrorIconDrawable之后调用TextInputLayout.setError

login_password_input_layout.setError("Error text!!");                
login_password_input_layout.setErrorIconDrawable(R.drawable....);

enter image description here

,

嗯,看看是否在下面直接使用

style="@style/Widget.Design.TextInputLayout"

然后您的错误drawable变为null。

我建议您创建一个样式,根据需要修改值

<style name="TextLabel" parent="Widget.Design.TextInputLayout">
    <item name="android:textColorHint">#f32</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">#4CAF50</item>
    <item name="errorIconDrawable">@drawable/ic_baseline_error_24</item>
    <item name="colorControlNormal">#673AB7</item>
    <item name="colorControlActivated">#E91E63</item>
    <item name="errorEnabled">true</item>
</style>

然后将其应用于

   <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password_input_layout"
        android:theme="@style/TextLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="username"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

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

这应该解决。