如何设置 TextInputLayout 错误消息颜色?
How to set TextInputLayout error message colour?
如何更改可设置为显示在 TextInputLayout
中文本字段下方的错误消息的颜色(通过 setError(...)
– see error state here)?
它通常显示为红色,我想更改。我应该在我的 styles.xml
文件中使用哪个项目 names/keys 来定位颜色?
提前致谢。
编辑:
已将 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/结果:
出现错误消息时,字段上方的提示会更改为与错误消息相同的颜色,覆盖提示颜色 - 这是设计使然。
在您的 styles.xml
文件中创建使用 @android:style/TextAppearance
作为父项的自定义样式:
<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
等)内的对象上设置提示,以保持不同的颜色提示和错误。
我需要动态地执行此操作。使用反射:
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)
。
更新
Please use a custom view instead and not this
@jared 的答案的修改版本适用于我的情况:
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();
}
}
实际上,要仅更改错误消息颜色,您可以在主题中设置 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 源代码,发现错误文本颜色来自 colors.xml。只需将此添加到您的 colors.xml:
<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>
边注。我已经用 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>
如果您正在使用 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>
根据需要,可以change/set TextInputLayout 文本颜色动态或直接在布局XML 文件中。下面是示例代码片段
创建自定义样式,使用 @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 小部件中使用它:
- Directly in XML Layout
<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">
- Dynamically in your class
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);
如果您想为您的应用程序设置 single/same 错误文本颜色,请在您的 应用主题
中定义文本颜色
<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>
使用 TextInputLayout
included in the Material Components Library 只需使用 app:errorTextColor
属性。
<com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>
在自定义样式中您可以使用:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>
如何更改可设置为显示在 TextInputLayout
中文本字段下方的错误消息的颜色(通过 setError(...)
– see error state here)?
它通常显示为红色,我想更改。我应该在我的 styles.xml
文件中使用哪个项目 names/keys 来定位颜色?
提前致谢。
编辑:
已将 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/结果:
出现错误消息时,字段上方的提示会更改为与错误消息相同的颜色,覆盖提示颜色 - 这是设计使然。
在您的 styles.xml
文件中创建使用 @android:style/TextAppearance
作为父项的自定义样式:
<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
等)内的对象上设置提示,以保持不同的颜色提示和错误。
我需要动态地执行此操作。使用反射:
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)
。
更新
Please use a custom view instead and not this
@jared 的答案的修改版本适用于我的情况:
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();
}
}
实际上,要仅更改错误消息颜色,您可以在主题中设置 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 源代码,发现错误文本颜色来自 colors.xml。只需将此添加到您的 colors.xml:
<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>
边注。我已经用 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>
如果您正在使用 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>
根据需要,可以change/set TextInputLayout 文本颜色动态或直接在布局XML 文件中。下面是示例代码片段
创建自定义样式,使用 @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 小部件中使用它:
- Directly in XML Layout
<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">
- Dynamically in your class
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);
如果您想为您的应用程序设置 single/same 错误文本颜色,请在您的 应用主题
中定义文本颜色<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>
使用 TextInputLayout
included in the Material Components Library 只需使用 app:errorTextColor
属性。
<com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>
在自定义样式中您可以使用:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>