Android 如何检查在edittext中输入的有效电子邮件格式
我已经创建了一个登录注册表单,我想在其中编辑文本以插入电子邮件地址我使用了输入类型文本电子邮件地址输入它不检查它是否是有效的电子邮件格式或不可以告诉如何在安卓中检查电子邮件格式 提前谢谢Android 如何检查在edittext中输入的有效电子邮件格式,android,android-edittext,Android,Android Edittext,我已经创建了一个登录注册表单,我想在其中编辑文本以插入电子邮件地址我使用了输入类型文本电子邮件地址输入它不检查它是否是有效的电子邮件格式或不可以告诉如何在安卓中检查电子邮件格式 提前谢谢 enter code here<EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lay
enter code here<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textEmailAddress" />
您可以使用正则表达式Regex检查电子邮件模式
Pattern pattern1 = Pattern.compile( "^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+");
Matcher matcher1 = pattern1.matcher(Email);
if (!matcher1.matches()) {
//show your message if not matches with email pattern
}
在其中传递电子邮件id:-
public static boolean emailAddressValidator(String emailId) {
Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*" + "\\@"
+ "\\w+([-.]\\w+)*" + "\\." + "\\w+([-.]\\w+)*");
Matcher matcher = pattern.matcher(emailId);
if (matcher.matches())
return true;
else
return false;
}
将EditText传递给此方法如果电子邮件地址有效,则将返回true,否则将返回false 这对于Android来说是非常好的表单编辑文本是EditText的扩展,它为EditText带来了数据验证功能 它为编辑文本值(例如电子邮件、号码、电话、信用卡等)提供自定义验证。 愿这对你有用
**Please follow the following Steps**
Seet - 1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@+id/textView_email"
android:layout_marginTop="40dp"
android:hint="Email Adderess"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Email Validation Example" />
</RelativeLayout>
公共类MainActivity扩展了活动{
private EditText email;
private String valid_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilizeUI();
}
/**
* This method is used to initialize UI Components
*/
private void initilizeUI() {
// TODO Auto-generated method stub
email = (EditText) findViewById(R.id.editText_email);
email.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Is_Valid_Email(email); // pass your EditText Obj here.
}
public void Is_Valid_Email(EditText edt) {
if (edt.getText().toString() == null) {
edt.setError("Invalid Email Address");
valid_email = null;
} else if (isEmailValid(edt.getText().toString()) == false) {
edt.setError("Invalid Email Address");
valid_email = null;
} else {
valid_email = edt.getText().toString();
}
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
} // end of TextWatcher (email)
});
}
}请参阅此
我引用的答案是最精确的,我相信这是最优雅的
在Android 2.2+上,使用以下命令:
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
方法1:android 2.2之后的后续工作
public final static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
方法2:使用正则表达式并向EditText的textChangeListener添加验证:
EdiText emailValidate;
String email = emailValidate.getEditableText().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
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
方法4
if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
edttextEmail.setError("Invalid Email Address");
}
您还可以使用blow表达式:
^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
您可以在此处查看详细的解决方案:
您可以使用正则表达式。不明白从网站上可以粘贴的代码检查下面的代码。将regx部件替换为链接中的部件。获取edittext值并与regexRepeated问题匹配可能重复hey我还有一个查询我还想检查日期验证还有谁告诉我如何进行日期验证什么类型的检查得到了伙计你能告诉我如何仅检查文本验证,如名称谢谢你提供了答案。请编辑您的答案,包括对代码的解释,好吗?这将有助于未来的读者更好地理解正在发生的事情,尤其是那些对该语言不熟悉并努力理解这些概念的社区成员。
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
edttextEmail.setError("Invalid Email Address");
}
^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
private TextInputLayout textInputPassword;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInputEmail = findViewById(R.id.text_input_email);
}
private boolean validateEmail() {
String emailInput = textInputEmail.getEditText().getText().toString().trim();
if (emailInput.isEmpty()) {
textInputEmail.setError("Field can't be empty");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
textInputEmail.setError("Please enter a valid email address");
return false;
} else {
textInputEmail.setError(null);
return true;
}
}