如何设置TextInputLayout错误信息的颜色?

如何更改可以设置为出现在TextInputLayout的文本字段下方的错误消息的颜色(通过setError(...) – 请参阅此处的错误状态 )?

它通常显示为红色,我想改变。 我应该在我的styles.xml文件中使用哪个项目名称/键来定位颜色?

提前致谢。

编辑:

向我的TextInputLayout添加了app:errorTextAppearance键:

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

和错误外观(设置为绿色进行testing)

 <style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance"> <item name="android:textColor">@android:color/holo_green_dark</item> </style> 

结果是提示以及错误消息是彩色的(来自缩放的Android模拟器的屏幕截图)

定期(没有错误):

在图像之前

错误状态:

在图像之后

编辑2 /结果:

出现错误消息时,字段上方的提示将变为与错误消息相同的颜色,覆盖提示颜色 – 这是devise的。

styles.xml文件中创build一个使用@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( EditTextTextView等)内部的对象上设置提示,以保存提示和错误的不同颜色。

实际上,只要改变错误信息的颜色,你可以在你的主题中设置textColorError (同时为通用控件和提示文本颜色设置colorControlNormalcolorControlActivated )。 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> 

我需要dynamic地做这个。 使用reflection:

 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的答案的修改版本在我的情况下工作:

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

我看了一下TextInputLayout的源代码,我意识到错误文本的颜色是从colors.xml中得到的。 只需将其添加到您的colors.xml中:

 <color name="design_textinput_error_color_light" tools:override="true">your hex color</color>