我想验证在 EditText 中引入的电子邮件以及我已经拥有的代码:
final EditText textMessage = (EditText)findViewById(R.id.textMessage);
final TextView text = (TextView)findViewById(R.id.text);
textMessage.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (textMessage.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && s.length() > 0)
{
text.setText("valid email");
}
else
{
text.setText("invalid email");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
问题是当我在“@”后面引入3个字符时,会出现“有效电子邮件”的消息,而当我引入完整的电子邮件时必须出现。
有什么建议吗?
谢谢大家!
最佳答案
只需将您的正则表达式更改如下:
"[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
因为 . (dot) 表示匹配任何单字符。在您的点之前添加一个双反斜杠以代表一个真正的点。
关于android - 验证 EditText 中的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625862/