如何在android中为EditText的错误文本编写样式?
我正在尝试为我的 android 应用程序编写新的自定义样式.我需要为在 EditText
中设置 setError
后出现的 errorText 赋予样式.
I'm trying to write new custom style for my android application. I need to give style to errorText which appears after setting setError
in EditText
.
如何自定义样式?
例如:我想在 style.xml 中设置它的 background
白色和 textColor
: Blue 等
For example: I want to set its background
white and textColor
: Blue etc etc. in style.xml
解决方案在最后,截图如下:
The solution is at the end and here is the screenshot:
一些解释
您可能可以使用以下行设置文本颜色
You might be able to set the textcolor using the following line
yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));
但是,这可能无法保证.
However, this might not be guaranteed.
根据源代码,显示的这个Popup
是ErrorPopup
类型,它是TextView
内部的一个类.这个 Popup
的内容是从 com.android.internal.R.layout.textview_hint
According to the source code, this Popup
that shows is of type ErrorPopup
which is an internal class inside TextView
. The content of this Popup
is a single TextView
inflated from com.android.internal.R.layout.textview_hint
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null);
这个Popup
的背景取决于它是否应该放在锚点之上:
The background of this Popup
depends on whether it should be placed above the anchor:
if (above) {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above);
} else {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error);
}
由于用于创建弹出窗口的所有 android 资源都是内部的并且最终是硬编码的,因此最好的方法是创建自己的错误弹出窗口.这将非常容易,而且您不会真正干扰正常的 EditText
,因为默认弹出窗口仅用于显示错误,因此,创建自己的弹出窗口就可以了.
Since all the android resources used to create the popup are internal and ultimately hard-coded, your best shot would be to create your own error popup. This would be very easy and you wouldn't really be interfering with the normal EditText
because the default popup is merely used to show the error, and, thus, creating your own would be fine.
解决方案
我在这里创建了它:WidgyWidgets