电子邮件和确认电子邮件匹配在Android中出错

时间:2014-05-12 11:50:23

标签: android regex pattern-matching

如果电子邮件和确认电子邮件不匹配,我想匹配电子邮件和confirm_email,那么它必须提供错误。如果两者都匹配,那么它一定不会显示为错误,但是无论我进入那里,它都将在下面的代码中“确认电子邮件不匹配”,任何人都可以告诉如何对电子邮件进行验证并确认电子邮件检查

    public class MainActivity extends Activity {
    String valid_email = null;
    String v5,v4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText editText3 = (EditText)findViewById(R.id.email);
    final EditText editText4 = (EditText)findViewById(R.id.confirm_email);

    editText3.addTextChangedListener(new TextWatcher() {    
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {  
            Is_Valid_Email_Address(editText3);
        }
    });
    editText4.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            }

        @Override
        public void afterTextChanged(Editable s) {

            Is_Valid_Confirm_Address(editText4);
        }
    });

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            v4 = editText3.getText().toString();
            v5 = editText4.getText().toString();  

public void Is_Valid_Confirm_Address(EditText editText4) {
    if (editText4.getText().toString() == null) {
        editText4.setError("Invalid Email Address");
        valid_email = null;
    } else if (isEmailValid(editText4.getText().toString()) == false) {
        editText4.setError("Invalid Email Address");
        valid_email = null;
    } else if (editText4.getText().toString() != v4) {
        editText4.setError("Confirm Email is Not Matching"); // if i enter the same address it will give this error
        valid_email = null;
    } else {
        valid_email = editText4.getText().toString();
    }

}

public void Is_Valid_Email_Address(EditText editText3) {
    if (editText3.getText().toString() == null) {
        editText3.setError("Invalid Email Address");
        valid_email = null;
    } else if (isEmailValid(editText3.getText().toString()) == false) {
        editText3.setError("Invalid Email Address");
        valid_email = null;
    } else {
        valid_email = editText3.getText().toString();
    }

}

private boolean isEmailValid(String email) {

    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}

2 个答案:

答案 0 :(得分:3)

试试这种方式

 if (editText3.getText().toString().equals(editText4.getText().toString())) {

    editText4.setError("Invalid Email Address");

 }else{

  editText4.setError("Email Address Match!!!!");
  }

用于.equals()比较的String方法

答案 1 :(得分:0)

比较字符串时,您应该.equals()而不是==