19

我使用 TextInputLayout,如果输入字段是必需的,我想以编程方式设置提示文本颜色和浮动标签颜色。在移动到 TextInputLayout 之前,我曾经使用以下代码以编程方式设置提示文本颜色

textField.setHintTextColor(Color.RED);

有人可以指导我如何以编程方式为 TextInputLayout 设置提示文本颜色和浮动标签颜色。

在随附的屏幕截图中,我希望提示文本地址 1在未聚焦时为红色,而在焦点上浮动标签地址 1应为红色。

在此处输入图像描述

4

8 回答 8

21

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

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

编辑 2018-08-01:

如果您使用的是设计库 v28.0.0 及更高版本,则字段已从mDefaultTextColortodefaultHintTextColor和 from mFocusedTextColorto更改focusedTextColor

检查其他字段的反编译类。

于 2016-09-01T12:07:14.443 回答
14

使用材料组件库,您可以使用:

在布局中:

<com.google.android.material.textfield.TextInputLayout
    app:hintTextColor="@color/mycolor"
    android:textColorHint="@color/text_input_hint_selector"
    .../>

风格:

<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <!-- The color of the label when it is collapsed and the text field is active -->
    <item name="hintTextColor">?attr/colorPrimary</item>
    <!-- The color of the label in all other text field states (such as resting and disabled) -->
    <item name="android:textColorHint">@color/mtrl_indicator_text_color</item>
</style>

在代码中:

// Sets the text color used by the hint in both the collapsed and expanded states
textInputLayout.setDefaultHintTextColor(...);

//Sets the collapsed hint text color
textInputLayout.setHintTextColor(....);
于 2019-09-06T12:25:44.393 回答
6

请好好看看这里的文档:TextInputLayout Methods

有一个方法:

setHintTextAppearance(int resId)

它需要一个可能是样式资源的资源 id!

我会试试这个,看看效果如何!

我希望它对你有帮助!

于 2016-02-28T14:19:48.470 回答
4

通常 TextInputLayout 提示文本颜色来自应用程序的 colorAccent。

但是,如果您想更改,则可以使用样式。

<android.support.design.widget.TextInputLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:theme="@style/TextLabel">
 </android.support.design.widget.TextInputLayout>

@风格

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/Color Name</item> 
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/Color Name</item>
    <item name="colorControlNormal">@color/Color Name</item>
    <item name="colorControlActivated">@color/Color Name</item>
 </style>

但是,如果您想添加红色,那么如何区分错误颜色意味着基本标准错误具有红色。

textField.setHintTextColor(Color.RED); 有人可以指导我如何以编程方式为 TextInputLayout 设置提示文本颜色和浮动标签颜色。

setHintTextColor 适用于 API 23+

于 2016-09-01T13:28:08.233 回答
3

用于同时更改Focused ColorDefault Text Color用于 TextInput 布局

private void setInputTextLayoutColor(int color, TextInputLayout textInputLayout) {
    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);

        Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(textInputLayout, myList);

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

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

编辑:更改 AppCompactEditText 线条颜色

您需要将backgroundTintList( 或supportBackgroundTintList)设置为仅包含您希望将色调更改为的颜色EditText的实例。ColorStateList以向后兼容的方式执行此操作的简单方法如下所示:

ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)

这将给出EditText所需的下划线颜色。

于 2017-05-17T04:32:20.920 回答
0

让我分享一下我在这方面的经验。我还尝试了在每个相关问题中给出的所有解决方案。即改变子部件的提示颜色为TextInputLayout

我很高兴能稍微详细地分享这个问题的答案。

我们只需要知道:-

  1. 将下一行添加到其中一个TextInputLayout或其子小部件的样式中并没有太大帮助。

    <item name="android:textColorHint">@color/white</item>

    因为每当焦点被接收/授予可编辑小部件时,它将使用 colorAccent。

  2. 这个问题的实际答案是在应用程序的样式标记中添加该样式行,当该或任何可编辑区域不在焦点时,它将设置提示颜色。(这是我们每次都错过的重点)。

如果我们对此有其他信息,请告诉我。

谢谢!

于 2018-11-12T07:25:42.747 回答
0

I was facing the quite same problem but with mutable hints and colored symbol. I'm made this trick with data binding and Spannable in this way.

 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/ti_last_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="8dp"
                app:boxStrokeWidth="0dp"
                app:boxStrokeWidthFocused="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_last_name"
                    style="@style/EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:onFocusChangeListener="@{viewModel.onFocusChangeListener(@string/last_name)}"
                    tools:hint="@string/last_name" />
</com.google.android.material.textfield.TextInputLayout>

viewModel.onFocusChangeListener defined in this way

    fun onFocusChangeListener(hint: String) =
        OnFocusChangeListener { view, isFocused ->
            (view.parent.parent as TextInputLayout).apply {
                val editText = (view as TextInputEditText)
                if (isFocused) {
                    this.hintTextColor = ColorStateList.valueOf(this.getColor(R.color.black))
                    editText.hint = ""
                    this.hint = hint
                } else {
                    if (!editText.text.isNullOrBlank()) {
                        this.defaultHintTextColor = ColorStateList.valueOf(this.getColor(R.color.black))
                        editText.hint = ""
                        this.hint = hint
                    } else {
                        this.hintTextColor = ColorStateList.valueOf(this.getColor(R.color.hint_color))
                        val builder = SpannableStringBuilder()
                        builder.append(hint)
                        val start = builder.length
                        val end = start + 1
                        builder.append("\u2981")
                        builder.setSpan(
                            ForegroundColorSpan(Color.RED),
                            start,
                            end,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        builder.setSpan(
                            SuperscriptSpan(),
                            start,
                            end,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        editText.hint = builder
                        this.hint = ""
                    }
                }
            }

        }

It's allow to use different hints and colors for focused/not focused states and has colored span.

this.getColor() it's just extension

 fun View.getColor(color: Int): Int {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        this.context.resources.getColor(color, context.theme)
    } else {
        this.context.resources.getColor(color)
    }
于 2020-07-14T14:37:52.403 回答
-5

我能够使用以下方法将 FloatingLabel 变为红色

textInputLayout.setErrorEnabled(true);
textInputLayout.setError(" ");

在此处输入图像描述

于 2016-02-28T15:24:03.620 回答