Android:TextInputLayout-自定义提示,底行和错误消息的颜色

时间:2019-08-23 15:56:51

标签: android xml android-layout android-design-library android-textinputlayout

我的布局看起来像这样:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="E-mail"
            android:imeOptions="actionDone"
            android:inputType="textEmailAddress"
            android:maxLines="1" />

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

</LinearLayout>

当我验证用户输入时,如果输入无效,则会添加如下错误消息:

email_container.setError("You must fill this field!");

结果是:

enter image description here

如您所见,并且正如预期的那样,提示底行错误消息都为红色

我的目标

提示提供分隔的颜色。那就是...使底线消息保持红色,但是当处于错误状态时,提示使用另一种颜色< / strong>。


已经尝试:

How to change the floating hint color of TextInputLayout if EditText inside is disabled

https://stackoverflow.com/a/53348723

https://stackoverflow.com/a/46765159

https://stackoverflow.com/a/33709380

...以及其他尝试解决android.support.design.widget.TextInputLayout包装简单EditText时的情况的解决方案。

但是,就我而言,我正在使用com.google.android.material.textfield.TextInputLayoutcom.google.android.material.textfield.TextInputEditText

设置错误后,我还尝试调用setErrorTextColorsetDefaultHintTextColor

email_container.setError("You must fill this field!");
email_container.setErrorTextColor(new ColorStateList(states, colors));
email_container.setDefaultHintTextColor(new ColorStateList(statesHint, colorsHint));

// Also tried with other states, but nothing
int[][] states = new int[][] {
        new int[] { android.R.attr.state_enabled},
        new int[] {-android.R.attr.state_enabled},
};

int[][] statesHint = new int[][] {
        new int[] { android.R.attr.state_enabled},
        new int[] {-android.R.attr.state_enabled},
};


int[] colors = new int[] {
        R.color.yellow,
        R.color.green,
};


int[] colorsHint = new int[] {
        R.color.black90,
        R.color.bt_text_blue,
};

但是,我得到的唯一的信息,结合了不同的状态,是使消息的颜色不同,但是提示底线,仍然具有相同的颜色。

有人知道如何处理吗?

1 个答案:

答案 0 :(得分:0)

我们可以自定义样式自定义TextInputLayout color。尝试使用此代码根据您的需要更改TextInputLayout颜色代码

style.xml

<style name="TextInputCustonColor" 
  parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <!-- The color of the label  -->
    <item name="android:textColorHint">@color/colorPrimaryGolden</item>
    <!-- The color of the label -->
    <item name="android:colorError" tools:targetApi="o">@color/colorError</item>
    <!--the color of text-->
    <item name="android:textColor">@color/colorPrimaryGolden</item>
    <!--the color of background-->
    <item name="boxBackgroundColor">@android:color/transparent</item>

</style>

activity.xml

    <android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:errorEnabled="true"
    style="@style/TextInputCustonColor">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        style="@style/TextInputCustonColor"
        android:imeOptions="actionDone"
        android:inputType="textEmailAddress"
        android:maxLines="1" />

</android.support.design.widget.TextInputLayout>