如何在TextInputEditText未聚焦时更改浮动提示颜色

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

enter image description here

当TextInputEditText被填充且未聚焦时,这种情况才会发生。我希望在TextInputEditText上方时提示文字为白色,在框内时为蓝色。 当TextInputEditText不为空且未聚焦时,提示文字颜色变为蓝色。

enter image description here

    <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来完成上述任务。


根据我的经验,您可以为非聚焦状态应用样式,但是如果字段为空或非空,效果是相同的。 - Gabriele Mariotti
7个回答

4
将以下代码添加到您的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>

在我的端上工作良好。只需检查它并让我知道结果即可。谢谢。


谢谢你的回答。运行得非常好。 - Sayok Majumder
我收到了 此组件的样式要求你的应用程序主题为Theme.MaterialComponents(或其子类)。 的错误提示。 - c0dehunter
你必须在gradle文件中使用“Material Design”依赖项。你添加了吗? - Akshay Sawant

1
只需在TextInputLayout中添加hintTextColor即可。
app:hintTextColor="@color/black"

1
在你的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>

0

我在这方面遇到了一些困难,所以我写了一个示例并尝试了几个方法,直到它正常工作。

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

<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 是提示样式。


0
添加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>

是的@Antonis Radz,我也做了同样的事情。但是 android:textColorHint="@color/app_blue" 这一行正在覆盖样式。 - Sayok Majumder
在我的情况下,当textColorHint没有焦点并且位于底部时,它是主要颜色,而自定义样式中的颜色在具有焦点且位于EditText上方时显示。 - Antonis Radz

0

我使用反射来改变焦点颜色。这是代码片段,希望能对某些人有所帮助。

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();
}

}


0
创建自定义主题用于 TextInputLayout,如下所示
<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"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接