如何在不聚焦时更改文本输入编辑文本下划线颜色?



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

我尝试了这个如何更改文本输入布局的编辑文本下划线 android 和那些(材料)编辑文本下划线颜色的颜色。

但不幸的是,它无法正常工作。

这是我今天尝试的最后一次尝试没有奏效:

<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>

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

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

我也遇到了类似的问题,与未聚焦相比,我的聚焦提示颜色不同。

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

在文本输入布局中,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>

如果要在未聚焦模式下设置大纲框的颜色而不是默认的黑色,则必须在color.xml文件中添加此行,该文件将覆盖轮廓框的默认颜色。

按原样复制此行。您可以将颜色更改为所需的颜色。

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

在这一点的大力帮助下,我设法让它工作。

所以基本上我所做的是用支持库AppCompatEditText替换EditText,并在上面设置背景色调,颜色是下划线颜色。

最后在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>

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

编辑

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

最新更新