Android EditText.setError() yields invisible error text

后端 未结 5 1215
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 21:17

I have a very simple EditText, as follows:



        
相关标签:
5条回答
  • 2020-12-08 21:33

    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

    0 讨论(0)
  • 2020-12-08 21:41

    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 讨论(0)
  • 2020-12-08 21:45

    For a more elegant solution try this one:

    editText.setError(Html.fromHtml("<font color='red'>Error Message!</font>"));
    
    0 讨论(0)
  • 2020-12-08 21:48

    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);
    
    0 讨论(0)
  • 2020-12-08 21:51

    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" />
    
    0 讨论(0)
提交回复
热议问题