我想更改无效电子邮件地址的错误消息的文本和背景颜色。我试过了,但我的短信没有显示任何内容。这是我的代码。
public class TextboxValidation {
//validating email address
public static boolean validateEditText(EditText editText) {
boolean valid = true;
Context context;
String text = editText.getText().toString();
boolean isEmail = (editText.getInputType() & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
boolean isNumeric = (editText.getInputType() & InputType.TYPE_NUMBER_FLAG_DECIMAL) == InputType.TYPE_NUMBER_FLAG_DECIMAL;
if (TextUtils.isEmpty(text)) {
if (!isNumeric || !TextUtils.isDigitsOnly(editText.getHint())) {
valid = false;
}
} else if (isEmail) {
valid = android.util.Patterns.EMAIL_ADDRESS.matcher(text).matches();
}
if (!valid) {
context = editText.getContext();
if (isEmail) {
int ecolor = R.color.black; // whatever color you want
String estring = "Veuillez saisir une addresse email valide";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
editText.setError(ssbuilder);
} else {
editText.setError("Le champ ne peut etre vide.");
}
return false;
}
editText.setError(null);
return true;
}
}