我有一个非常简单的EditText,如下所示:
<EditText
android:id="@+id/myedit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="32"/>
在某些验证码中,我使用Android的EditText.setError()
来显示任何验证错误。这在OS 2.x中工作正常,但在OS 3.x设备(Xoom)上没有 - 在Xoom上,您可以看到错误弹出的大纲,但是您看不到错误文本。
我猜测文本在那里,但它是不可见的。如何让它可见?我没有看到与错误文本相关的android:textColor
。
此外,如果文本确实是不可见的,那么为什么2.x与3.x的行为不同的任何想法 - 似乎这会导致向后兼容性问题。
感谢。
答案 0 :(得分:49)
看起来您可以通过使用SpannableStringBuilder对象而不是String调用EditText.setError()来解决此问题。
int ecolor = xxxx; // whatever color you want
String estring = "Input is incorrect";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
myedittext.setError(ssbuilder);
答案 1 :(得分:18)
要获得更优雅的解决方案,请尝试以下方法:
editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));
答案 2 :(得分:6)
使用原生parent theme以适合您自定义样式的API级别! “@android:风格/ theme.name”
按Configuration qualifier区分主题:
value/style.xml - > parent="@android:style/Theme.Light"
value-v11/style.xml -> parent="@android:style/Theme.Holo.Light"
value-v14/style.xml -> parent="@android:style/Theme.DeviceDefault.Light"
答案 3 :(得分:4)
我遇到了同样的问题。在我的例子中,我已将parent =“android:Theme.Light”应用于值-V14 / styles.xml。这使得EditText控件看起来像是来自android API 2.3.3及更低版本的控件。但错误消息文本全是白色,因此不可见。经过一番头疼后,我想出了这个。
将parent =“android:Theme.Light”更改为parent =“android:Theme.Holo.Light.NoActionBar”(什么是Holo主题)。但是在EditText定义中添加android:background="@android:drawable/edit_text"
我的EditText看起来像这样
<EditText
android:id="@+id/password"
style="@style/editTextNormal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:background="@android:drawable/edit_text" />
答案 4 :(得分:4)
如果要更改错误文本视图的文本颜色,则应在主题文件中添加此代码。
对于v8:
<item name="android:textColorSecondaryInverse">@android:color/secondary_text_light</item>
对于v11:
<item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>