任何人都可以帮助我在此代码中进行电子邮件验证吗? 我尝试了很多东西,但它没有显示任何响应...... 请帮忙... 谢谢你。
代码:
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");
}
}
});
最佳答案
这样做
在你的 textWatcher 上
public void afterTextChanged(Editable arg0) {
if(!isValidEmail(etfeedbackemail.getText().toString())){
etfeedbackemail.setError("Enter a valid address");
}
}
检查电子邮件在其他返回 bool 值的方法上是否有效
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();
}
}
关于android - 使用文本观察器进行电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495040/