有人可以帮助我在此代码中进行电子邮件验证吗? 我尝试了很多东西,但它没有显示任何反应...... 请帮忙... 谢谢你。
代码:
etfeedbackemail.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if (etfeedbackemail.getText().toString() == "") {
etfeedbackemail.setError("Please enter your email.");
}
if (etfeedbackemail.getText().toString().contains(".*[^a-z^0-9].*")) {
etfeedbackemail.setError("Enter a valid address");
}
}
});
答案 0 :(得分:2)
这样做
public void afterTextChanged(Editable arg0) {
if(!isValidEmail(etfeedbackemail.getText().toString())){
etfeedbackemail.setError("Enter a valid address");
}
}
检查电子邮件是否在其他返回boolean
的方法上有效public final static boolean isValidEmail(String target) {
if (target == null) {
return false;
} else {
//android Regex to check the email address Validation
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
答案 1 :(得分:0)
使用此代码对我有用 -
替换你的代码行 -
`etfeedbackemail.setError("Please enter your email.");`
这些行
Drawable errorIcon = getResources().getDrawable(R.drawable.error_icon);
etfeedbackemail.setError(Html.fromHtml("<font color='white'>"+errorMSG+"</font>"),errorIcon);
一切顺利......
答案 2 :(得分:0)
检查这个答案
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
emailValidate .addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (email.matches(emailPattern) && s.length() > 0)
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
// or
textView.setText("valid email");
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
//or
textView.setText("invalid email");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// other stuffs
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// other stuffs
}
});
答案 3 :(得分:0)
您可以使用Androids电子邮件模式。这是Kotlin中的代码,你可以在JAVA中做类似的
etfeedbackemail.addTextChangedListener(object:TextWatcher{
override fun afterTextChanged(s: Editable?) {
// Does nothing intentionally
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Does nothing intentionally
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val email = s.toString()
val isValid = android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
if (!isValid) {
etfeedbackemail.setError("Invalid Email")
} else {
etfeedbackemail.setError(null)
}
}
})