如何更改可设置为显示在
TextInputLayout
中文本字段下方的错误消息的颜色(通过 setError(...)
– 在此处查看错误状态)?
它通常显示为红色,我想改变它。我应该在
styles.xml
文件中使用哪些项目名称/键来定位颜色?
向我的
app:errorTextAppearance
添加了 TextInputLayout
键:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="@+id/welcome_current_week_container"
app:errorTextAppearance="@style/WelcomeErrorAppearance">
<EditText
..../>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
和错误外观(设置为绿色进行测试):
<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>
结果是提示和错误消息都是彩色的 (来自缩放的 Android 模拟器的屏幕截图):
常规(无错误):
错误状态:
编辑2/结果:
出现错误消息时,字段上方的提示将更改为与错误消息相同的颜色,覆盖提示颜色 - 这是设计使然。
创建一个自定义样式,在您的
@android:style/TextAppearance
文件中使用 styles.xml
作为父级:
<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/red_500</item>
<item name="android:textSize">12sp</item>
</style>
并在您的 TextInputLayout 小部件中使用它:
<android.support.design.widget.TextInputLayout
android:id="@+id/emailInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">
编辑: 在 TextInputLayout 内部的对象上设置提示(
EditText
、TextView
等),为提示和错误保留不同的颜色。
实际上,要仅更改错误消息颜色,您可以在主题中设置
textColorError
(还可以为常规小部件和提示文本颜色设置 colorControlNormal
和 colorControlActivated
)。 TextInputLayout
选择该属性。 注意: 如果您将 errorTextAppearance
设置为自定义样式,则 textColorError
将不起作用。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/control_normal</item>
<item name="colorControlActivated">@color/control_activated</item>
<item name="textColorError">@color/error</item>
<!-- other styles... -->
</style>
在您的 AndroidManifest.xml 中:
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- ... -->
</application>
TextInputLayout
包含在材料组件库中,只需使用app:errorTextColor
属性即可。
<com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>
在自定义样式中,您可以使用:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>
旁注。我已经尝试过使用
errorTextAppereance
接受的解决方案。
它工作得很好,但一开始,应用新的 errorTextAppereance
样式后,输入下划线颜色没有改变。我看到有一些评论,其他人也遇到了同样的问题。
就我而言,当我在设置新的错误文本后设置新样式时,就会发生这种情况。像这样:
passwordInputLayout.error = "Password strength"
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
切换这两种方法的顺序后,文本和下划线颜色按预期发生变化。
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
passwordInputLayout.error = "Password strength"
错误文本外观样式看起来像这样:
<style name="InputError" parent="TextAppearance.Design.Error"/>
<style name="InputError.Purple">
<item name="android:textColor">@color/purple</item>
</style>
我需要动态地执行此操作。使用反射:
public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
fCurTextColor.setAccessible(true);
fCurTextColor.set(mErrorView, color);
} catch (Exception e) {
e.printStackTrace();
}
}
在调用上述方法之前,您需要调用
textInputLayout.setErrorEnabled(true)
才能使其工作。
更新
请使用自定义视图而不是这个
@jared's Answer 的修改版本适用于我的情况:
public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView)fErrorView.get(textInputLayout);
mErrorView.setTextColor(color);
mErrorView.requestLayout();
} catch (Exception e) {
e.printStackTrace();
}
}
如果您使用 com.google.android.material.textfield.TextInputLayout 此输入布局,那么您只需设置一种样式
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutPassword"
style="@style/LoginTextInputLayoutStyle"
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">@color/text_input_box</item>
<item name="errorTextColor">@color/colorRed</item>
</style>
根据需要,可以动态或直接在布局 XML 文件中更改/设置 TextInputLayout 文本颜色。以下是示例代码片段
创建一个自定义样式,使用 @android:style/TextAppearance 作为 styles.xml 文件中的父级:
<style name="style_error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/color_error</item>
<item name="android:textSize">11sp</item>
</style>
并且,在您的 TextInputLayout 小部件中使用它:
- 直接在 XML 布局中
<android.support.design.widget.TextInputLayout
android:id="@+id/your_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/style_error_appearance">
- 在课堂上充满活力
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);
如果您想为您的应用程序设置单一/相同的错误文本颜色,请在您的 应用程序主题中定义文本颜色
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Error text color... -->
<item name="textColorError">@color/color_error</item>
<!-- other styles... -->
</style>
在您的 AndroidManifest.xml 中:
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/your_app_name">
<!-- ... -->
</application>
我是从代码中设置的
til.setErrorTextColor(ColorStateList.valueOf(errorColor))
我查看了 TextInputLayout 源代码,发现错误文本颜色是从 color.xml 获取的。只需将其添加到您的 color.xml 中即可:
<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>