我创建了一个登录注册表单,其中我想编辑文本以插入我曾使用输入类型文本电子邮件地址的电子邮件地址,因为它不检查天气是否是有效的电子邮件格式,可以告诉我们如何检查电子邮件在android中的格式 提前谢谢
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" />
答案 0 :(得分:8)
请参阅此how to check edittext's text is email address or not?
我引用最常见的答案,我认为这是最优雅的。
在Android 2.2+上使用此:
boolean isEmailValid(CharSequence email) { return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); }
答案 1 :(得分:2)
您可以使用正则表达式(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
}
答案 2 :(得分:1)
**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>
Seet - 2 import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText;
Seet - 3
公共类MainActivity扩展了Activity {
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)
});
}
}
答案 3 :(得分:1)
方法-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");
}
答案 4 :(得分:0)
在其中传递电子邮件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;
}
答案 5 :(得分:0)
将EditText传递给此方法,如果电子邮件地址有效,则返回true
,否则返回false
/**
* method is used for checking valid email id format.
*
* @param email
* @return boolean true for valid false for invalid
*/
public static boolean isEmailAddressValid(String email) {
boolean isEmailValid = false;
String strExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern objPattern = Pattern.compile(strExpression , Pattern.CASE_INSENSITIVE);
Matcher objMatcher = objPattern .matcher(inputStr);
if (objMatcher .matches()) {
isEmailValid = true;
}
return isEmailValid ;
}
答案 6 :(得分:0)
Android表单编辑文本非常好link
是EditText的扩展,它为编辑文本提供了数据验证工具。
它提供编辑文本值的自定义验证(示例 - 电子邮件,电话号码,电话,信用卡等) 愿这对你有用..
答案 7 :(得分:0)
您还可以使用打击表达:
^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
答案 8 :(得分:0)
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;
}
}
你可以在这里查看详细的解决方案: https://codinginflow.com/tutorials/android/validate-email-password-regular-expressions