0

我创建了一个登录注册表单,我想在其中编辑文本以插入电子邮件地址我使用了输入类型文本电子邮件地址放置它不检查天气它是有效的电子邮件格式,或者无法告诉如何在 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" />
4

9 回答 9

12

参考这个如何检查edittext的文本是否是电子邮件地址?

我引用了最打勾的答案,我相信它是最优雅的。

在 Android 2.2+ 上使用这个:

boolean isEmailValid(CharSequence email) {
   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
于 2015-02-17T15:38:18.627 回答
3

您可以使用正则表达式 (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
}
于 2013-05-29T11:01:46.023 回答
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)
    });

}

}

于 2014-05-20T05:37:22.217 回答
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 
    } 
}); 

方法三

    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");

                   }
于 2016-11-15T14:46:53.300 回答
0

这里非常link适合 Android 表单编辑文本是 EditText 的扩展,它为 edittext 带来了数据验证工具。

它为编辑文本值(例如 - 电子邮件、号码、电话、信用卡等)提供了自定义验证。希望这对您有用..

于 2013-05-29T11:10:40.600 回答
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;
}
于 2013-05-29T11:04:00.547 回答
0

将 EditText 传递给此方法,true如果 E-mail 地址有效,则返回,否则返回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 ;
}
于 2013-05-29T11:05:10.973 回答
0

您也可以使用打击表达:

^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
于 2018-09-10T04:50:30.940 回答
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

于 2021-02-02T18:18:11.453 回答