如何更改材质设计TextInputLayout提示文本颜色?

问题描述

我正在尝试将Material Design的hintTextColor的{​​{1}} AND 设置为BoxstrokeColor三种颜色,例如:

  • 红色表示何时textInputLayout(我不知道如何将disabled设置为BoxstrokeColor状态,所以请不要介意截图)

enter image description here

  • 蓝色disabled但为enabled

enter image description here

  • 绿色表示何时unfocusedenabled

enter image description here

我该怎么做?

对于focused,我尝试了Gabriele Mariottihere中提出的建议,但是问题是颜色之一应用于两种不同的状态([{{1 }}]和[hintTextColor,但disabled]),我想区分这两个。

解决方法

您可以使用自定义样式:

<style name="CustomOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
    <item name="android:textColorHint">@color/text_color_hint</item>
    <item name="hintTextColor">@color/green</item>
</style>

使用@color/text_color_hint选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
    <item android:alpha="..." android:color="@color/blue"/>
</selector>

@color/text_input_layout_stroke_color选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="..." android:color="@color/green" android:state_focused="true"/>
    <item android:alpha="..." android:color="@color/green" android:state_hovered="true"/>
    <item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
    <item android:alpha="..." android:color="@color/blue"/>  <!-- unfocused -->
</selector>

重点:

enter image description here

不专心:

enter image description here

已禁用:

enter image description here