如何在密码长度上显示错误消息?通过Firebase身份验证进行登录。如果最后是EditText,则mpassword.mpassword.setError(“密码必须介于8到15个字符之间。”); 返回; 我试图在密码长度之外做出一个if / else语句,但这似乎并没有奏效。 这是我到目前为止的代码。
package com.debatewithus.ui.login;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.debatewithus.MainActivity;
import com.debatewithus.R;
import com.debatewithus.User;
import com.debatewithus.UserLocalStore;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class LoginActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState, ClassNotFoundException task) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
Button login;
Button sign_up;
final EditText mail;
final EditText mpassword;
TextView forgotpassword;
final FirebaseAuth auth;
auth = FirebaseAuth.getInstance();
mail = findViewById(R.id.username);
mpassword = findViewById(R.id.Password);
login = findViewById(R.id.login);
sign_up = findViewById(R.id.signup);
forgotpassword = findViewById(R.id.forgotpassword);
login.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}));
sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email_address = mail.getText().toString().trim();
String password = mpassword.getText().toString().trim();
if (TextUtils.isEmpty(email_address)) {
mail.setError("Email is Required.");
return;
}
if (TextUtils.isEmpty(password)) {
mpassword.setError("Password Required.");
return;
if (!(password.length() < 8 && (password.length() > 15))) {
final EditText mpassword.mpassword.setError("Password Must be Between 8 and 15 Characters.");
return;
}
auth.signInWithEmailAndPassword(email_address, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful())
startActivity(new Intent(getApplicationContext(), MainActivity.class) {
});
else {
Toast.makeText(LoginActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
};
});
};
};
});
};
};
答案 0 :(得分:1)
您需要与||
进行逻辑或,而不是与&&
进行逻辑与:
if (!(password.length() < 8 || (password.length() > 15)))
如果密码长度小于8或大于15,则计算结果为true。
答案 1 :(得分:0)
用OR替换AND,然后删除! 。那应该做。
if(((password.length()<8 ||(password.length()> 15)))){