InputTextLayout,将错误文本设置为帮助文本(不同颜色)
InputTextLayout, setting Error Text as Helper Text (of different color)
Android 提供了一个名为 TextInputLayout 的小部件
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
这可以提供提示和错误,这是对 EditText 的出色包装。
https://material.google.com/patterns/errors.html#errors-user-input-errors
中似乎也有相关指南
然后,它显示提示和错误。而且它还有帮助文本。这是对密码设置有用的地方。例如。在有人输入密码之前,ErrorText 显示为 HelperText,用不同的颜色区分(根据示例为灰色而不是红色)。
当我查看 TextInputLayout 时,它确实具有这两个功能
setError(CharSequence error)
和
setHint(CharSequence hint)
但是,无法将 ErrorText 更改为 HelperText。我是否遗漏了什么,或者只是支持库提供的 TextInputLayout 没有为我们提供其 material 指南中推荐的 Google 功能?
p/s: 我知道如何通过样式设置来改变错误文本的颜色。但这无济于事,因为当应用程序处于 运行 时它无法动态更改(例如,将状态从错误更改为帮助程序,反之亦然)。
鉴于设计指南中提到了此方案,它不是标准方法的一部分有点奇怪。无论如何,我能够根据@Jared Rummler 在 上的回答更改颜色。他的建议是使用 Java 反射来处理脏的部分。首先,您需要获取对 TextInputLayout
的引用并启用错误视图(否则反射将 return 空字段):
final TextInputLayout input = (TextInputLayout) findViewById(R.id.input);
input.setErrorEnabled(true);
现在调用定义错误颜色改变方法如下:
public 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's
下划线和错误消息的颜色。但是,根据指南,它们无论如何都是相同的,所以这符合我们的场景。这意味着要显示我们的帮助文本,我们只需将颜色更改为灰色并调用 setError()
,如下所示:
setErrorTextColor(input, ContextCompat.getColor(getContext(), android.R.color.darker_gray));
input.setError("Funk you!");
应该可以了。当您希望显示错误时,请记住将颜色更改为红色。
找到了这个 TextInputLayout 的扩展版本。
https://gist.github.com/drstranges/1a86965f582f610244d6
这设法自然地添加帮助文本。
您可以在布局上使用 app:errorTextAppearance
或 textLayout.setErrorTextAppearance()
来更改错误文本的样式。
如果您将其设置为与帮助文本相匹配的样式,那么它将看起来像帮助文本。然后使用 "error" 方法显示您的文本
textLayout.setErrorTextAppearance(R.style.TextAppearance_HelperText);
textLayout.setErrorEnabled(true)
textLayout.setError(getString(errorText));
正在访问的人
使用 Design Support Library 28,在 TextInputLayout.It 中添加了一个内置的辅助文本功能,也可以使用内置方法更改颜色。
setHelperTextColor(ColorStateList textColors)
添加
即可使用
implementation 'com.android.support:design:28.0.0'
现在使用 xml 或以编程方式
启用错误
textInputLayout.isHelperTextEnabled=true
textInputLayout.error="Email can not be Empty!!!"
此外,提示和错误现在可以一起使用了!!!
示例
et.setOnFocusChangeListener { v, b ->
if (b) {
textInputLayout.helperText = "yourhelperText"
} else {
textInputLayout.helperText = null
if(et.text.toString()==""){ // or any other validation
textInputLayout.error="Email can not be Empty!!!"
}
}
TextInputLayout | Android Developers
EDIT 不要忘记通过 xml 或以编程方式启用错误和 helperText。
Android 提供了一个名为 TextInputLayout 的小部件 https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
这可以提供提示和错误,这是对 EditText 的出色包装。
https://material.google.com/patterns/errors.html#errors-user-input-errors
中似乎也有相关指南然后,它显示提示和错误。而且它还有帮助文本。这是对密码设置有用的地方。例如。在有人输入密码之前,ErrorText 显示为 HelperText,用不同的颜色区分(根据示例为灰色而不是红色)。
当我查看 TextInputLayout 时,它确实具有这两个功能
setError(CharSequence error)
和
setHint(CharSequence hint)
但是,无法将 ErrorText 更改为 HelperText。我是否遗漏了什么,或者只是支持库提供的 TextInputLayout 没有为我们提供其 material 指南中推荐的 Google 功能?
p/s: 我知道如何通过样式设置来改变错误文本的颜色。但这无济于事,因为当应用程序处于 运行 时它无法动态更改(例如,将状态从错误更改为帮助程序,反之亦然)。
鉴于设计指南中提到了此方案,它不是标准方法的一部分有点奇怪。无论如何,我能够根据@Jared Rummler 在 TextInputLayout
的引用并启用错误视图(否则反射将 return 空字段):
final TextInputLayout input = (TextInputLayout) findViewById(R.id.input);
input.setErrorEnabled(true);
现在调用定义错误颜色改变方法如下:
public 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's
下划线和错误消息的颜色。但是,根据指南,它们无论如何都是相同的,所以这符合我们的场景。这意味着要显示我们的帮助文本,我们只需将颜色更改为灰色并调用 setError()
,如下所示:
setErrorTextColor(input, ContextCompat.getColor(getContext(), android.R.color.darker_gray));
input.setError("Funk you!");
应该可以了。当您希望显示错误时,请记住将颜色更改为红色。
找到了这个 TextInputLayout 的扩展版本。 https://gist.github.com/drstranges/1a86965f582f610244d6
这设法自然地添加帮助文本。
您可以在布局上使用 app:errorTextAppearance
或 textLayout.setErrorTextAppearance()
来更改错误文本的样式。
如果您将其设置为与帮助文本相匹配的样式,那么它将看起来像帮助文本。然后使用 "error" 方法显示您的文本
textLayout.setErrorTextAppearance(R.style.TextAppearance_HelperText);
textLayout.setErrorEnabled(true)
textLayout.setError(getString(errorText));
正在访问的人 使用 Design Support Library 28,在 TextInputLayout.It 中添加了一个内置的辅助文本功能,也可以使用内置方法更改颜色。
setHelperTextColor(ColorStateList textColors)
添加
即可使用 implementation 'com.android.support:design:28.0.0'
现在使用 xml 或以编程方式
启用错误 textInputLayout.isHelperTextEnabled=true
textInputLayout.error="Email can not be Empty!!!"
此外,提示和错误现在可以一起使用了!!!
示例
et.setOnFocusChangeListener { v, b ->
if (b) {
textInputLayout.helperText = "yourhelperText"
} else {
textInputLayout.helperText = null
if(et.text.toString()==""){ // or any other validation
textInputLayout.error="Email can not be Empty!!!"
}
}
TextInputLayout | Android Developers
EDIT 不要忘记通过 xml 或以编程方式启用错误和 helperText。