gpt4 book ai didi

java - 验证电子邮件不适用于 textInputLayout

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:42:38 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com