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

时间:2019-05-10 14:42:00

标签: android android-textinputlayout android-textinputedittext

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

我尝试过how to change color of TextinputLayout's edittext underline android和那些(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>

我的问题是为下划线和提示文本颜色提供一种简单的解决方案。

我的目标是,当文本输入布局为FOCUSED并且很好时,我在这里使用的功能。 现在,我还想为未设置文本输入布局时设置它。

3 个答案:

答案 0 :(得分:1)

我也有一个类似的问题,我的重点提示颜色与未重点提示的颜色不同。

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

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

答案 1 :(得分:0)

thisthat的大力帮助下,我设法使它开始工作。

所以基本上我所做的就是用支持库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 dir hierarhy有关。无论出于什么原因,我那里都有一些古老的未使用值-v21,并且里面有一些样式。今天,我做了一些重构,看到了它,只是删除了整个目录,只留下了一种样式(默认值是dir),然后所有样式开始工作。我猜想,如果我将正确的样式复制到此v21中就可以使用,但是我根本不需要它。

答案 2 :(得分:0)

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

照原样复制这一行。您可以将颜色更改为您想要的颜色。

<color name="mtrl_textinput_default_box_stroke_color">#fff</color>
相关问题