0

我遇到了 TextInputLayout 错误状态的问题。当我在 TextInputLayout 上设置错误时,错误消息是红色的,还有下划线,但提示没有(它保持默认颜色 - 灰色)。我已经设置了errorTextAppearance,但我似乎无法让提示文本改变颜色。

<android.support.design.widget.TextInputLayout
            android:id="@+id/textInputLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="My Hint"
            app:errorTextAppearance="@style/MyErrorTextAppearance">

<android.support.design.widget.TextInputEditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:maxLength="10"
                android:maxLines="1" />

样式.xml

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MyErrorTextAppearance" parent="TextAppearance.Design.Error">
    <item name="android:textColor">@color/colorError</item>
</style>

我设置并清除错误如下:

myTextInputLayout.setErrorEnabled(true);
myTextInputLayout.setError(errorMessage);

myTextInputLayout.setError(null);
myTextInputLayout.setErrorEnabled(false);
4

1 回答 1

0

尝试以下代码:

fun setHintColor(@ColorInt color: Int)
{
    try {
        var stateList = ColorStateList(arrayOf(intArrayOf(0)), intArrayOf(color))
        val defaultTextColorField = TextInputLayout::class.java.getDeclaredField("mDefaultTextColor")
        defaultTextColorField?.let {field->
            field.isAccessible = true
            field.set(this, stateList)
        }
        val focusedTextColorField = TextInputLayout::class.java.getDeclaredField("mFocusedTextColor")
        focusedTextColorField?.let{field->
            field.isAccessible = true
            field.set(this, stateList)
        }
    }catch(e: java.lang.Exception){
        // handle exception
    }
}
于 2019-02-28T18:15:07.740 回答