我有我添加了监听器的textInputLayout。我正在尝试检查输入的电子邮件是否有效。为此,我写了下面的代码。但它不起作用。即使我给出正确的电子邮件ID,它也会显示有效的电子邮件ID。
package com.hari.ga.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.hari.ga.lending.R;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Shiva on 17-10-2015.
*/
public class Personal extends AppCompatActivity {
protected Button next;
EditText editText;
String e;
Boolean check;
protected TextInputLayout emailText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal_details);
editText = (EditText)findViewById(R.id.email);
next = (Button)findViewById(R.id.next_button);
emailText = (TextInputLayout)findViewById(R.id.emailText);
e = editText.getText().toString();
emailText.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean c = checkEmail(e);
if (!c) {
emailText.setError("valid email id is required");
emailText.setErrorEnabled(true);
} else {
emailText.setErrorEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
/* editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(e)) {
Snackbar.make(v, e, Snackbar.LENGTH_SHORT).show();
emailText.setErrorEnabled(false);
} else {
emailText.setError("Input required");
emailText.setErrorEnabled(true);
}
}
}); */
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(check==false) {
Toast.makeText(getApplicationContext(), "Please make sure the details are right", Toast.LENGTH_LONG).show();
} else{
Intent intent = new Intent(Personal.this, HomeDetails.class);
startActivity(intent);
}
}
});
}
public static boolean checkEmail(String email) {
Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
}
答案 0 :(得分:2)
这应该有效,
android.util.Patterns.EMAIL_ADDRESS.matcher(emailText.getText().toString()).matches())
答案 1 :(得分:1)
试试这个功能......
public boolean isValidEmailAddress(String email) {
String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
java.util.regex.Matcher m = p.matcher(email);
return m.matches();
}
答案 2 :(得分:1)
代码无效,因为您正在检查TextChangedListener中的String e实例,该实例是在onCreate方法中指定的,这意味着您没有检查EditText中的更新文本。无论EditText发生什么变化,它总是检查String实例,因此总是返回true。
您需要做的是从EditText获取更新的文本并使用它来运行您的检查功能。你可以这样做
@Override
public void afterTextChanged(Editable s) {
String updatedText = s.toString();
// do your check on updatedText here
}
答案 3 :(得分:0)
解决了:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");
if(!EMAIL_ADDRESS_PATTERN.matcher(s).matches())
{
emailText.setError("valid email id is required");
emailText.setErrorEnabled(true);
} else {
emailText.setErrorEnabled(false);
}
}
答案 4 :(得分:0)
试试这个
private boolean validateEmail() {
String email = inputEmail.getText().toString().trim();
if (email.isEmpty() || !isValidEmail(email)) {
inputLayoutEmail.setError(getString(R.string.err_msg_email));
requestFocus(inputEmail);
return false;
} else {
inputLayoutEmail.setErrorEnabled(false);
}
return true;
}