Seb*_*hec 74 android android-textinputlayout
如何更改可以设置为显示在文本字段下方的错误消息的颜色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>
Run Code Online (Sandbox Code Playgroud)
和错误外观(设置为绿色进行测试):
<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)
结果是提示以及错误消息被着色(来自缩放的Android模拟器的屏幕截图):
常规(无错误):
错误状态:
编辑2 /结果:
出现错误消息时,字段上方的提示将更改为与错误消息相同的颜色,覆盖提示颜色 - 这是设计使然.
dab*_*248 129
创建一个@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>
Run Code Online (Sandbox Code Playgroud)
并在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">
Run Code Online (Sandbox Code Playgroud)

编辑:对象,这是你的TextInputLayout(内部上设置提示EditText,TextView等等)持有不同颜色的提示和错误.
Vic*_*ani 28
实际上,要仅更改错误消息颜色,您可以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>
Run Code Online (Sandbox Code Playgroud)
在你的AndroidManifest.xml中:
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- ... -->
</application>
Run Code Online (Sandbox Code Playgroud)
我需要动态地这样做.使用反射:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
textInputLayout.setErrorEnabled(true)在调用上述方法之前,您需要调用此方法才能工作.
随着TextInputLayout包括在材料零件库只需使用app:errorTextColor属性。
<com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>
Run Code Online (Sandbox Code Playgroud)
在自定义样式中,您可以使用:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
边注。我已经尝试了一种接受的解决方案errorTextAppereance。它确实很好用,但是起初,在应用新errorTextAppereance样式后,输入下划线颜色没有改变。我看到有一些评论,其他人也遇到了同样的问题。
就我而言,这是在设置新错误文本后设置新样式时发生的。像这样:
passwordInputLayout.error = "Password strength"
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
Run Code Online (Sandbox Code Playgroud)
切换这两种方法的顺序后,文本和下划线颜色将按预期变化。
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
passwordInputLayout.error = "Password strength"
Run Code Online (Sandbox Code Playgroud)
错误文本的外观样式如下所示:
<style name="InputError" parent="TextAppearance.Design.Error"/>
<style name="InputError.Purple">
<item name="android:textColor">@color/purple</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51580 次 |
| 最近记录: |