Vorrei suggerire di usare @Codeversed solution, ma se non va bene per voi per qualche motivo riesco a utilizzare l'implementazione personalizzata EditText
.
rappresentazione EditText usuale:
EditText con l'errore:
In poche parole: ho creato Stato XML personalizzato per la visualizzazione degli errori. Vedere codice relativo seguito:
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;
}
}
drawable/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>
drawable/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>
aggiungere al vostro attrs.xml
<attr name="errorColor" format="reference"/>
e styleables.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="error">
<attr name="state_error" format="boolean"/>
</declare-styleable>
</resources>
e il loro utilizzo è davvero semplice:
<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]:
Appena realizzato, la risposta originale riguardava la modifica del colore del popup di errore, ma non il colore di sfondo di EditText. Ad ogni modo, spero che questo possa aiutare qualcuno.
Hai dato un'occhiata a [questa domanda] (http://stackoverflow.com/questions/6745577/which-theme-attribute-changes-the-text-color-of-an-edittexts-error-message)? – Eric
L'ho fatto, in realtà - ma voglio cambiare il colore della bolla, non il colore del testo (che sono in grado di modificare). Ho notato che Chris è riuscito a cambiare lo sfondo - non ho idea di come. – scana
Sembra che 'errorMessageBackground' sia un nuovo attributo introdotto nel livello API Android 19. Hai provato a mettere il tuo stile nella cartella' values-v19'? –