我有一个loginActivity
班。进行email
和password
验证,并检查登录字段是否为空。
问题是,如果登录字段为空,我的应用程序将停止。
这是我的代码:
验证者:
private boolean validatePassword() {
boolean password1 = password.getText().toString().trim().isEmpty();
String password2 = password.getText().toString().trim();
if (password1) {
password.setError("Поле не должно быть пустым");
return false;
} else if (!PASSWORD_PATTERN.matcher(password2).matches()) {
password.setError("Слабый пароль. ");
return false;
} else {
password.setError(null);
return true;
}
}
private boolean validateEmail() {
String email1 = email.getText().toString().trim();
if (email1.isEmpty()) {
email.setError("Поле не должно быть пустым");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(email1).matches()) {
email.setError("Введите правильный E-mail");
return false;
} else {
email.setError(null);
return true;
}
}
注册:
public void register(final String email, final String password) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (validateEmail() & validatePassword()) {
if (task.isSuccessful()) {
Toast.makeText(LogInActivity.this, "Successfully", Toast.LENGTH_SHORT).show();
} else Toast.makeText(LogInActivity.this, "Failed", Toast.LENGTH_SHORT).show();
} else if (email.isEmpty() || password.isEmpty())
Toast.makeText(LogInActivity.this, "Поля не должно быть пустыми", Toast.LENGTH_SHORT).show();
}
});
}
登录:
public void logIn(final String email, final String password) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (checkBox.isChecked()) {
if (validateEmail()) {
if (task.isSuccessful()) {
Toast.makeText(LogInActivity.this, "Successfully", Toast.LENGTH_SHORT).show();
startNewActivity();
} else
Toast.makeText(LogInActivity.this, "Failed", Toast.LENGTH_SHORT).show();
} else if (email.isEmpty() || password.isEmpty())
Toast.makeText(LogInActivity.this, "Проверьте введенные данные", Toast.LENGTH_SHORT).show();
;
}
}
});
}
更改活动:
private void startNewActivity() {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
this.finish();
}
答案 0 :(得分:0)
我无法在您的代码中找到错误,因为您没有发布任何logcat错误消息,但是我也进行了类似的操作,并且它对我来说非常有效,只需更改诸如editTextPassword,editTextEmail之类的字段以及您的意图领域,你也很好。
private void userLogin() {
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (email.isEmpty()) {
editTextEmail.setError("Email is required");
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError("Please enter a valid email");
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()) {
editTextPassword.setError("Password is required");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
editTextPassword.setError("Minimum lenght of password should be 6");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
finish();
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
您只需在用户单击登录按钮时调用此功能。
Button loginButton = (Button)findViewById(R.id.buttonLogin);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userLogin();
}
});