Android 未聚焦时如何更改TextInputItemText浮动提示颜色

Android 未聚焦时如何更改TextInputItemText浮动提示颜色,android,styling,android-textinputlayout,Android,Styling,Android Textinputlayout,我试图创建一个TextInputInputText,它的浮动提示颜色在不聚焦时不会改变。 提示的颜色从白色变为蓝色,即通过textColorHint字段设置的颜色 只有当TextInputItemText已填充且未聚焦时,才会发生这种情况。我希望提示在文本输入文本上方为白色,在框内为蓝色。 当文本输入文本不为空且未聚焦时,提示颜色变为蓝色 textinputtext <com.google.android.material.textfield.TextInputLayout

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

只有当TextInputItemText已填充且未聚焦时,才会发生这种情况。我希望提示在文本输入文本上方为白色,在框内为蓝色。 当文本输入文本不为空且未聚焦时,提示颜色变为蓝色

textinputtext

    <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>
    <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/white</item>
    </style>

TextAppearance.App.TextInputLayout

    <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>
    <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/white</item>
    </style>

@android:彩色/白色

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

添加
app:hintTextAppearance
风格如下:

   <com.google.android.material.textfield.TextInputLayout
    ......
      app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
    ......>
android:theme="@style/InputText.Light"
<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>


顶色

我通过反射改变了聚焦颜色。下面是一个片段,它可能会帮助一些人

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

文本输入布局创建自定义主题,如下所示


将此代码添加到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.Filled Box.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>


对我来说效果很好。检查一下,让我知道。谢谢。

在文本输入布局中添加样式

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

我的风格是这样的:

   <com.google.android.material.textfield.TextInputLayout
    ......
      app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
    ......>
android:theme="@style/InputText.Light"
<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>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@风格/髓鞘1
@风格/我的提示
#1111D

基本上,
@style/myedit1
指向
TextInputLayout
样式,在那里,
hintTextAppearance
指向提示样式

是的@Antonis Radz我也做过同样的事。但是
android:textColorHint=“@color/app_blue”
行正在覆盖样式。嗯,在我的例子中,textColorHint在没有焦点且位于底部时是原色,而自定义样式中的颜色在有焦点且高于EditTextIn时显示。根据我的经验,您可以为未聚焦状态应用样式,但是字段是否为空都是一样的。谢谢你的回答。工作原理类似于charmI。我得到
此组件上的样式要求您的应用程序主题为theme.MaterialComponents(或后代)。
。您必须在gradle文件中对此使用“Material Design”依赖项。你加进去了吗?