开发者

Android EditText.setError() yields invisible error text

开发者 https://www.devze.com 2023-04-01 17:08 出处:网络
I have a very simple EditText, as follows: <EditText android:id=\"@+id/myedit\" android:layout_width=\"fill_parent\"

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开发者_如何学C 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.


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);


For a more elegant solution try this one:

editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));


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


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" />


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消