作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有 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();
}
}
最佳答案
这应该可以,
android.util.Patterns.EMAIL_ADDRESS.matcher(emailText.getText().toString()).matches())
关于java - 验证电子邮件不适用于 textInputLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33183329/
我是一名优秀的程序员,十分优秀!