如何在未获得焦点时更改 TextInputEditText 浮动提示颜色

How to change the TextInputEditText floating hint color when not focused

我正在尝试创建一个 TextInputEditText,其浮动提示颜色在未聚焦时不会改变。 提示的颜色从白色变为蓝色,即变为通过 textColorHint 字段设置的颜色。

只有在 TextInputEditText 已填充且未获得焦点时才会发生。我希望提示在 TextInputEditText 上方时为白色,在框内时为蓝色。 当 TextInputEditText 不为空且未聚焦时,提示颜色变为蓝色。

TextInputEditText

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="4dp"
        android:textColorHint="@color/app_blue"
        app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/fNameET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/text_input_layout_background"
            android:ellipsize="end"
            android:hint="First Name"
            android:lines="1"
            android:maxLines="1"
            android:paddingStart="16dp"
            android:paddingEnd="16dp" />
</com.google.android.material.textfield.TextInputLayout>

TextAppearance.App.TextInputLayout

    <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/white</item>
    </style>

而且我想使用 xml 来完成上述任务而不是 java。

添加 app:hintTextAppearance 样式如下:

   <com.google.android.material.textfield.TextInputLayout
    ......
      app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
    ......>

 <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">top color</item>
  </style>

更新:

您还需要像这样设置 EditText 主题:

android:theme="@style/InputText.Light"

 <style name="InputText.Light">
        <item name="android:textColorHighlight">top color</item>
    </style>

我用反射改变了聚焦颜色。这是可能对某人有帮助的片段。

private void setUpperHintColor(int color) {
try {
    Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
    field.setAccessible(true);
    int[][] states = new int[][]{
            new int[]{}
    };
    int[] colors = new int[]{
            color
    };
    ColorStateList myList = new ColorStateList(states, colors);
    field.set(textInputLayout, myList);

    Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
    method.setAccessible(true);
    method.invoke(textInputLayout, true);

} catch (Exception e) {
    e.printStackTrace();
}

}

create custom theme for TextInputLayout like below

<style name="TextInputLayoutHint" parent="TextAppearance.AppCompat">
    <item name="colorAccent">@android:color/white</item>
    <item name="android:textColorHint">@color/BackgroundtWhiteColor</item>
    <item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
    <item name="colorControlActivated">@color/your color</item>
    <item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>
</style>

并将其应用于 TextInputLayout,如下所示。

<android.support.design.widget.TextInputLayout
            android:theme="@style/TextInputLayoutHint"/>

将此代码添加到您的 style.xml 文件中 ->

<style name="TextLabelDummy" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
        <item name="colorAccent">@color/black</item>  // Text Color
        <item name="android:textColorHint">@color/blue</item>    // Hint Color
        <item name="colorControlActivated">@color/white</item>    //Floating label color
    </style>

您可以根据需要更改父属性。我随机使用了 "Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"

然后将其作为主题调用到您的布局文件中

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayoutNumberDummy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:layout_marginBottom="32dp"
            android:theme="@style/TextLabelDummy">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditTextNumberDummy"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/text_mobile" />

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

在我这边效果很好。只需检查一下,然后让我知道。谢谢。

在您的 TextInputLayout 中添加样式

 <style name="TextLabel">
        <item name="android:textColorHint">@color/dark_gray</item>
        <item name="colorControlActivated">@color/color_Black</item>
        <item name="android:focusable">false</item>
        <item name="android:focusableInTouchMode">true</item>
   </style>

我对此有点挣扎,所以我写了一个示例并尝试了一些东西,直到我开始工作。

我的布局没有什么特别之处:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/user_agent_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="hint text"
        android:importantForAutofill="noExcludeDescendants" />
</com.google.android.material.textfield.TextInputLayout>

我的风格是这样的:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="textInputStyle">@style/myedit1</item>
</style>


<style name="myedit1" parent="Widget.Design.TextInputLayout">
    <item name="hintTextAppearance">@style/myHint</item>

</style>

<style name="myHint" parent="@style/TextAppearance.Design.Hint">
    <item name="android:textColor">#1111dd</item>
</style>

基本上 <item name="textInputStyle">@style/myedit1</item> 指向 TextInputLayout 样式,hintTextAppearance 指向提示样式。

只需在 TextInputLayout 中添加 hintTextColor

app:hintTextColor="@color/black"