我在下面写了代码。 首先,我希望看到只有一个TextInputLayout
我正在使用的错误。 其次,合规密码的错误没有显示,我可以做些什么来解决这个问题?
public class DoctorSignUp extends AppCompatActivity implements View.OnClickListener {
TextView tvSubmit;
EditText etEmailId, etPassword, etCPassword, etMobile;
FirebaseAuth firebaseAuth;
TextInputLayout tilEmailId,tilPassword,tilCPassword,tilMobile;
ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_signup);
//initialize view controls
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
etEmailId = (EditText) findViewById(R.id.etEmailId);
etPassword = (EditText) findViewById(R.id.etPassword);
etCPassword = (EditText) findViewById(R.id.etCPassword);
etMobile = (EditText) findViewById(R.id.etMobile);
tvSubmit = (TextView) findViewById(R.id.tvSubmit);
tilEmailId= (TextInputLayout) findViewById(R.id.tilEmailId);
tilPassword= (TextInputLayout) findViewById(R.id.tilPassword);
tilCPassword= (TextInputLayout) findViewById(R.id.tilCPassword);
tilMobile= (TextInputLayout) findViewById(R.id.tilMobile);
//event handling
tvSubmit.setOnClickListener(this);
etEmailId.addTextChangedListener(new MyTextWatcher(DoctorSignUp.this));
etPassword.addTextChangedListener(new MyTextWatcher(DoctorSignUp.this));
etCPassword.addTextChangedListener(new MyTextWatcher(DoctorSignUp.this));
etMobile.addTextChangedListener(new MyTextWatcher(DoctorSignUp.this));
}
@Override
public void onClick(View v) {
registerUser();
}
private void registerUser() {
String email = etEmailId.getText().toString();
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Please enter email address", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Please enter password", Toast.LENGTH_SHORT).show();
return;
}
progressDialog.setMessage("Registering User,Please wait..");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Registration Successful", Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(getApplicationContext(), "Failed To Register The User", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
private class MyTextWatcher implements TextWatcher {
public MyTextWatcher(Context context) {
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
validateEmail();
validatePasswword();
validateConformPassword();
validateMobile();
}
private void validateConformPassword() {
String password = etPassword.getText().toString();
String cpassword=etCPassword.getText().toString();
if(!password.equals(cpassword)){
tilCPassword.setError("Please enter the same password again");
}
else
tilCPassword.setErrorEnabled(false);
}
private void validateMobile() {
String mobile = etMobile.getText().toString();
if(mobile.length()<10) {
tilMobile.setError("Enter a valid mobile no.");
}
else
tilMobile.setErrorEnabled(false);
}
private void validatePasswword() {
String password = etPassword.getText().toString();
if (password.length() <6){
tilPassword.setError("Password must be of 6 characters atleast");
}
else
tilPassword.setErrorEnabled(false);
}
private void validateEmail() {
String email = etEmailId.getText().toString();
if(!email.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")){
tilEmailId.setError("Please enter valid email id");
}
else
tilEmailId.setErrorEnabled(false);
}
}
}
答案 0 :(得分:1)
试试这个
您可以根据需要验证所有文本
private class MyTextWatcher implements TextWatcher {
public MyTextWatcher(Context context) {
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.etEmailId:
validateEmail();
break;
case R.id.etPassword:
validatePasswword();
break;
case R.id.etCPassword:
validatePasswword();
validateConformPassword();
break;
case R.id.etMobile:
validateMobile();
break;
}
}
使用equalsIgnoreCase匹配字符串
private void validateConformPassword() {
String password = etPassword.getText().toString();
String cpassword=etCPassword.getText().toString();
if(!password.equalsIgnoreCase(cpassword)){
tilCPassword.setError("Please enter the same password again");
}
else
tilCPassword.setErrorEnabled(false);
}
答案 1 :(得分:1)
尝试这种方法。
public boolean validate() {
boolean valid = true;
enteredEmailId = mEmail.getText().toString();
enteredPassword = mPassword.getText().toString();
if (enteredEmailId.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(enteredEmailId).matches()) {
mEmail.setError("Please Enter Valid Email Address");
valid = false;
} else {
mEmail.setError(null);
}
if (enteredPassword.isEmpty() || enteredPassword.length() < 4 || enteredPassword.length() > 10) {
mPassword.setError("Password should be between 4 to 10 alphanumeric characters");
valid = false;
} else {
mPassword.setError(null);
}
return valid;
}
}