我们如何能够执行电子邮件验证
在的EditText
在机器人
?我已经虽然谷歌和放大器;左右,但不力找出最简单的验证它的方式。
How can we perform Email Validation
on edittext
in android
? I have gone though google & SO but dint find out simplest way to validate it.
要完成电子邮件验证,我们有很多方法,但简单的&放大器;最简单的方法是两种方法
To perform Email Validation we have many ways,but simple & easiest way are two methods.
1 - 使用的EditText(....)addTextChangedListener
为什么老触发每个输入在的EditText箱
即email_id无效或有效
1- Using EditText(....).addTextChangedListener
which keeps triggering on every input in an EditText box
i.e email_id is invalid or valid
/**
* Email Validation ex:- tech@end.com
*/
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
}
});
2 - 使用的if-else
情况最简单的方法。以使用gettext()的EditText上盒串并提供了电子邮件的模式进行比较。如果模式不匹配或macthes,按钮的onClick敬酒的消息。它会不会在盒子的EditText一个字符的每个输入触发。下面示出简单的例子。
2- Simplest method using if-else
condition. Take the EditText box string using getText() and compare with pattern provided for email. If pattern doesn't match or macthes, onClick of button toast a message. It ll not trigger on every input of an character in EditText box . simple example shown below.
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]+";
// onClick of button perform this simplest code.
if (email.matches(emailPattern))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}