TextInputLayout 下划线颜色在聚焦时不适应自定义颜色

phi*_*lip 5 android android-textinputlayout textinputlayout material-components material-components-android

不知道我错过了什么,但每次edittext聚焦时,下划线颜色都不会适应我设置的自定义颜色。这是我的主题代码供参考

<style name="EditTextHintWhite" parent="@style/AppTheme">
   <item name="color">@color/white</item>
   <item name="android:textColorHint">@color/white</item>
   <item name="colorControlNormal">@color/white</item>
   <item name="colorControlActivated">@color/white</item>
   <item name="colorError">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已将它们全部设置为白色,但不知何故,当焦点edittext聚焦时,下划线变成绿色

在此输入图像描述

这是我的布局代码

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

        <androidx.appcompat.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:shadowColor="@color/white"
            android:singleLine="true"
            android:textColor="@color/white"
            app:backgroundTint="@color/white" />

</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 4

FilledBox 样式的下划线颜色由boxStrokeColor属性定义。您可以将其添加到布局或自定义样式中。就像是:

<style name="...." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
    <!-- underline color in FilledBox style -->
    <item name="boxStrokeColor">@color/custom_selector_filled_stroke_color</item>
    ....
</style>
Run Code Online (Sandbox Code Playgroud)

这是选择器的默认值:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <!-- 4% overlay over 42% colorOnSurface -->
  <item android:alpha="0.46" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.42" android:color="?attr/colorOnSurface"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

聚焦时使用的颜色是第一行 <item android:color="?attr/colorPrimary" android:state_focused="true"/>

在此输入图像描述

在您的代码中使用(删除app:backgroundTint中的EditText

    <com.google.android.material.textfield.TextInputLayout
        android:hint="@string/email"
        android:theme="@style/EditTextHintWhite"
        ..>

        <com.google.android.material.textfield.TextInputEditText
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:textColor="@color/white"
            ../>

    </com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

与(删除父级):

  <style name="EditTextHintWhite">
    <item name="colorError">@color/white</item>
    <item name="colorPrimary">@color/...</item>
    <item name="colorOnSurface">@color/...</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

如果您想要自定义下划线,请app:boxStrokeColor="@color/text_input_layout_stroke_color"使用TextInputLayout.

注意:使用com.google.android.material.textfield.TextInputEditText代替androidx.appcompat.widget.AppCompatEditText.