2013-01-02 13 views
30

Chcę zmienić kolor tła (ustawianie niestandardowego losowania) w wyskakującym komunikacie o błędzie wyświetlanym po użyciu metody setError().Zmień tło komunikatu o błędzie EditText

Obecnie wygląda to tak:

enter image description here

Odkryłam, że Android ma dwa pliki:

  • popup_inline_error.9.png
  • popup_inline_above_error.9.png

a ty Powinno być możliwe ustawić je przy użyciu dwóch atrybutów:

  • errorMessageBackground
  • errorMessageAboveBackground

Ale gdy próbuję ustawić je w moim temacie, wszystko pojawia się:

<item name="errorMessageBackground">@drawable/popup_inline_error_holo_light</item> 
<item name="errorMessageAboveBackground">@drawable/popup_inline_error_above_holo_light</item> 

error: Error: No resource found that matches the given name: attr 'errorMessageBackground'. 

(jest to ten sam with android:errorMessageBackground)

Wstawiam to q w tym przypadku, bo skończyły mi się pomysły - może ktoś już to zrobił?

EDIT: Nagłówek tematu używam:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style 
     name="Theme.MyThemeName" 
     parent="@style/Theme.Sherlock.Light"> 

KOLEJNY EDIT: Uh, odkryłem, że moje pytanie jest duplikatem: android:errorMessageBackground getting no resource found error in styles.xml

JEDNA KOLEJNA EDYCJA: Jest to znany problem, spójrz na ten link: https://code.google.com/p/android/issues/detail?id=55879

+1

Spojrzeliście na [to pytanie] (http://stackoverflow.com/questions/6745577/which-theme-attribute-changes-text-color-of-an-edittexts-error-message)? – Eric

+0

Tak, rzeczywiście, ale chcę zmienić kolor bańki, a nie kolor tekstu (który jestem w stanie zmienić). Zauważyłem, że Chris zdołał zmienić tło - nie mam pojęcia, jak to zrobić. – scana

+0

Wygląda na to, że 'errorMessageBackground' jest nowym atrybutem wprowadzonym na poziomie 19 interfejsu Androida API. Czy próbowałeś umieścić swój styl w folderze' values-v19'? –

Odpowiedz

0
private EditText adTitle; 
// .... 
adTitle.setError(Html.fromHtml("<font color='red'>hello</font>")); 
+0

To, czego chcę, to zmienić tło EditText, a nie sam kolor czcionki - przed odpowiedzią przeczytaj uważnie to pytanie. – scana

+1

To było naprawdę przydatne dla mnie ... !!! dzięki..!!! –

+16

To nie odpowiada na pytanie. – Garcon

-2

Można użyć tej metody po prostu przekazać tekst MSG swój identyfikator EditText

public static void setErrorMsg(String msg,EditText viewId) 
{ 
    //Osama ibrahim 10/5/2013 
    int ecolor = Color.WHITE; // whatever color you want 
    String estring = msg; 
    ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor); 
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring); 
    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0); 
    viewId.setError(ssbuilder); 

} 
+3

Niestety, to nie jest to, co chciałem - musiałem zmienić całe tło, aby wyglądało tak samo jak komunikat o błędzie z ICS. – scana

5

trzeba będzie zawierać te dependancies:

compile 'com.android.support:appcompat-v7:23.1.1' 
compile 'com.android.support:design:23.1.1' 

A oto próbka, w jaki sposób użyj go:

<android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/input_password" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/hint_email" /> 

</android.support.design.widget.TextInputLayout> 

Dzięki temu uzyskasz projekt materiału, którego szukasz, aby uzyskać weryfikację formularza, a także ładny efekt animacji dla etykiety.

enter image description here

4

Proponuję użyć @Codeversed solution, ale jeśli to nie pasuje do ciebie z jakiegoś powodu można używać realizację niestandardowych EditText.

Usual reprezentacja EditText: enter image description here

EditText z błędem: enter image description here

W kilku słowach: I utworzeniu niestandardowego stanu xml do wyświetlania błędu. Zobacz kod związany poniżej:

InputEditText.java:

import android.annotation.TargetApi; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.os.Build; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.AttributeSet; 
import android.widget.EditText; 

import com.example.oleksandr.inputedittext.R; 

/** 
* Input EditText which allows define custom drawable for error state 
*/ 
public class InputEditText extends EditText { 

    private static final int[] STATE_ERROR = {R.attr.state_error}; 

    private boolean mIsError = false; 

    public InputEditText(Context context) { 
     this(context, null, 0); 
     init(); 
    } 

    public InputEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public InputEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public InputEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(); 
    } 

    private void init() { 
     addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       // empty 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       setError(null); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // empty 
      } 
     }); 
    } 

    @Override 
    public void setError(CharSequence error) { 
     mIsError = error != null; 
     super.setError(error); 
     refreshDrawableState(); 
    } 

    @Override 
    public void setError(CharSequence error, Drawable icon) { 
     mIsError = error != null; 
     super.setError(error, icon); 
     refreshDrawableState(); 
    } 

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
     final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 
     if (mIsError) { 
      mergeDrawableStates(drawableState, STATE_ERROR); 
     } 
     return drawableState; 
    } 
} 

rozciągliwej/edittext_bg_error.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    android:id="@+id/listview_background_shape" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <stroke 
     android:width="2dp" 
     android:color="#f00" 
     /> 
    <padding 
     android:bottom="2dp" 
     android:left="2dp" 
     android:right="2dp" 
     android:top="2dp" 
     /> 
    <corners android:radius="5dp"/> 
    <solid android:color="#ffffffff"/> 
</shape> 

rozciągliwej/edittext_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <!-- custom error state drawable --> 
    <item android:drawable="@drawable/edittext_bg_error" app:state_error="true"/> 

    <!-- Do whatever you want for all other states --> 
    <item android:drawable="@android:drawable/editbox_background_normal"/> 
</selector> 

dodać do attrs.xml

<attr name="errorColor" format="reference"/> 

i styleables.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="error"> 
     <attr name="state_error" format="boolean"/> 
    </declare-styleable> 
</resources> 

i użycie jest bardzo proste:

<com.example.oleksandr.inputedittext.views.InputEditText 
    android:id="@id/edittext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/edittext_bg_selector" 
    android:inputType="text" 
    android:text="@string/hello_world" 
    /> 

[EDIT]:

Właśnie zdałem sobie sprawę, że oryginalna odpowiedź dotyczyła zmiany koloru wyskakującego o błędzie, ale nie o kolorze tła EditText. W każdym razie, mam nadzieję, że to może komuś pomóc.

+1

Szukałem czegoś podobnego do tego podejścia. dzięki –

Powiązane problemy