Android EditText.setError() yields invisible error

2020-02-03 05:58发布

I have a very simple EditText, as follows:

<EditText
     android:id="@+id/myedit"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:maxLength="32"/>

In some validation code, I use Android's EditText.setError() to show any validation errors. This works fine in OS 2.x but not on an OS 3.x device (Xoom) -- on the Xoom you can see the outline of the error popup but you cannot see the error text.

I'm guessing that the text is there, but it is invisible. How do I make it visible? I don't see an android:textColor that would relate to error text.

Also, if the text is indeed invisible, then any ideas why 2.x behaves differently to 3.x -- seems like this would cause backward-compatibility problems.

Thanks.

5条回答
来,给爷笑一个
2楼-- · 2020-02-03 06:14

If you want to change the text color of the error text view, then you should add this code in your theme files.

For v8:

<item name="android:textColorSecondaryInverse">@android:color/secondary_text_light</item>

For v11:

<item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>
查看更多
我想做一个坏孩纸
3楼-- · 2020-02-03 06:17

It looks like you can work around this problem by calling EditText.setError() with a SpannableStringBuilder object rather than a String.

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);
查看更多
虎瘦雄心在
4楼-- · 2020-02-03 06:21

For a more elegant solution try this one:

editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));
查看更多
Melony?
5楼-- · 2020-02-03 06:21

I had the same problem. In my case, I had applied the parent="android:Theme.Light" to values-V14/styles.xml. This made the EditText control to look like a control from android API 2.3.3 and below. But the error message text was all white and hence, was not visible. After some head scratching I figured this out.

Change the parent="android:Theme.Light" to parent="android:Theme.Holo.Light.NoActionBar" (what ever Holo theme). But in the EditText definition add android:background="@android:drawable/edit_text"

My EditText looks like this

<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" />
查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-03 06:23

Use native parent theme for appropriate API level in your own customized style! "@android:style/theme.name".

Distinguish themes by 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"

http://code.google.com/p/android/issues/detail?id=22920

查看更多
登录 后发表回答