未聚焦时如何更改textinput edittext下划线颜色?

How to change textinput edittext underline color when unfocused?

我正在使用 android 设计库的 TextinputLayout。但是无法在TextinputLayout 中自定义EditText 的下划线颜色。请帮忙。

我试过这些 and those (Material) EditText underline color .

但不幸的是无法让它工作。

这是我最后一次尝试,但今天没有成功:

<android.support.design.widget.TextInputLayout
            android:id="@+id/tilPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tilLogin"
            android:layout_marginBottom="@dimen/login_line_v_margin"
            app:hintTextAppearance="@style/TextAppearence.App.TextInputLayoutLight">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/passwordHint"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

和样式:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="TextAppearence.App.TextInputLayoutLight" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorTealLight</item>
        <item name="android:textSize">18sp</item>
        <item name="colorControlNormal">#fff</item>
        <item name="colorControlActivated">@color/colorTealLight</item>
    </style>

我的问题是为下划线和提示文本颜色找到一个干净的解决方案。

我的目标是,当文本输入布局被聚焦并且它很好时,我在这里的工作。 现在我也想在文本输入布局未聚焦时设置它。

this and that 的大力帮助下,我设法让它工作了。

基本上我所做的就是用支持库 AppCompatEditText 替换 EditText 并在其上设置 backgroundTint 颜色为下划线颜色。

最后在 TextInputLayout 上设置 textColorHint,颜色为提示文本颜色。

像这样:

<android.support.design.widget.TextInputLayout
            android:id="@+id/tilPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tilLogin"
            android:layout_marginBottom="@dimen/login_line_v_margin"
            app:hintTextAppearance="@style/TextAppearence.App.TextInputLayoutLight"
            android:textColorHint="@color/colorLoginHint">

            <android.support.v7.widget.AppCompatEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/passwordHint"
                android:textColor="@color/colorLoginHint"
                android:textSize="@dimen/login_font_minor"
                android:textStyle="normal"
                android:inputType="textPassword"
                android:backgroundTint="@color/colorTealLight"/>
        </android.support.design.widget.TextInputLayout>

也许不那么漂亮,但它确实有效。

编辑

原来这个问题与我损坏的 res 目录层次结构有关。无论出于何种原因,我都有一些古老的未使用的值-v21,并且它内部有一些样式。今天我正在做一些重构并看到它并删除了整个目录并只留下一种样式(默认值目录中的那些)然后所有样式都开始工作。我想如果我将正确的样式复制到这个 v21 它会起作用,但我根本不需要它。

我也有类似的问题,我的聚焦提示颜色与未聚焦的提示颜色不同。

对我有用的解决方案如下:

在您的文本输入布局中,将 android:textColorHint="@color/your_unfocused_color" 设置为未聚焦颜色,将 app:hintTextColor="@color/your_focused_color" 设置为聚焦颜色,这也会在聚焦状态下更改下划线描边颜色。

下划线需要设置app:boxStrokeColor="@color/underline_colors"

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/focused" android:state_enabled="true" />
    <item android:color="@color/focused" android:state_hovered="true" />
    <item android:color="@color/focused" android:state_focused="true" />
    <item android:color="@color/unfocused" />
</selector>

如果您想在未聚焦模式下设置轮廓框的颜色而不是默认的黑色,您必须在 colors.xml 文件中添加此行,这将覆盖轮廓框的默认颜色。

按原样复制这一行。您可以根据需要更改颜色。

<color name="mtrl_textinput_default_box_stroke_color">#fff</color>