我面临的问题是用户输入没有经过验证。当我单击regSignUp2Btn按钮时,它将直接跳转到下一个屏幕。当用户没有提供验证信息时,它应该在TextInputLayout下提供一个错误消息。我的代码有错误吗?下面是我的代码。Java:
public class signUpScreen extends AppCompatActivity {
//Variables
TextInputLayout regEmail, regName, regPassword, regConfirmPassword;
TextInputEditText emailEt, nameEt, passwordEt, confirmPasswordEt;
Button regSignUp2Btn, regToSignInBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sign_up_screen);
regEmail = findViewById(R.id.emailAdd);
regName = findViewById(R.id.name);
regPassword = findViewById(R.id.password);
regConfirmPassword = findViewById(R.id.confirmPassword);
regSignUp2Btn = findViewById(R.id.goToSignUp2Btn);
regToSignInBtn = findViewById(R.id.regToSignInBtn);
emailEt = findViewById(R.id.emailAddET);
nameEt = findViewById(R.id.nameET);
passwordEt = findViewById(R.id.passwordET);
confirmPasswordEt = findViewById(R.id.confirmPasswordET);
regSignUp2Btn.setOnClickListener(v -> {
//Get all the values
String name = regName.getEditText().getText().toString().trim();
String email = regEmail.getEditText().getText().toString().trim();
String password = regPassword.getEditText().getText().toString();
Intent intent = new Intent(signUpScreen.this, signUpScreen2.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
startActivity(intent);
});
regToSignInBtn.setOnClickListener(view -> {
Intent intent = new Intent(signUpScreen.this, signInScreen.class);
startActivity(intent);
});
}
private boolean validateName() {
String val = regName.getEditText().getText().toString().trim();
if (val.isEmpty()) {
regName.setError("Field cannot be empty");
return false;
} else if (val.length() > 30) {
regName.setError("Username too long");
return false;
} else {
regName.setError(null); //remove error
regName.setErrorEnabled(false); //remove space
return true;
}
}
private boolean validateEmail() {
String val = regEmail.getEditText().getText().toString();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+.+[a-z]+";
String noWhiteSpace = "\\A\\w{1,30}\\z";
if (val.isEmpty()) {
regEmail.setError("Field cannot be empty");
return false;
} else if (!val.matches(emailPattern)) {
regEmail.setError("Invalid email address");
return false;
} else if (!val.matches(noWhiteSpace)) {
regEmail.setError("White spaces are not allowed");
return false;
} else {
regEmail.setError(null);
regEmail.setErrorEnabled(false);
return true;
}
}
private boolean validatePassword() {
String val = regPassword.getEditText().getText().toString();
String passwordVal = "^" +
"(?=.*[0-9])" + //at least 1 digit
"(?=.*[a-z])" + //at least 1 lower case letter
"(?=.*[A-Z])" + //at least 1 upper case letter
"(?=.*[a-zA-Z])" + //any letter
"(?=.*[@#$%^&+=])" + //at least 1 special character
"(?=\\S+$)" + //no white spaces
".{4,}" + //at least 4 characters
"$";
if (val.isEmpty()) {
regPassword.setError("Field cannot be empty");
return false;
} else if (!val.matches(passwordVal)) {
regPassword.setError("Password is too weak. Should have 1 symbol, 1 digit, 1 lower case, 1 upper case and at least 4 characters");
return false;
} else {
regPassword.setError(null);
regPassword.setErrorEnabled(false);
return true;
}
}
private boolean validateConfirmPassword() {
String val = regPassword.getEditText().getText().toString();
String val1 = regConfirmPassword.getEditText().getText().toString();
if (val.isEmpty()) {
regConfirmPassword.setError("Field cannot be empty");
return false;
} else if (!val.equals(val1)) {
regPassword.setError(null);
regConfirmPassword.setError(null);
regPassword.setErrorEnabled(false);
regConfirmPassword.setErrorEnabled(false);
regConfirmPassword.setError("Password is not same");
return false;
} else {
return true;
}
}
public void call2ndSignUpScreen(View view) {
if (!validateEmail() | !validateName() | !validatePassword() | !validateConfirmPassword()) {
return;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".signUpScreen"
android:background="@color/white"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/signup_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_baseline_arrow_back_ios_24"
app:tint="@color/black"
android:contentDescription="@string/backbtn" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="227dp"
android:layout_height="117dp"
android:layout_gravity="center"
android:contentDescription="@string/todo"
app:srcCompat="@drawable/crop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/create_your_account"
android:textSize="12sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailAdd"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_address"
app:boxBackgroundColor="@color/white"
app:boxStrokeColor="@color/black"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/black"
app:startIconDrawable="@drawable/ic_baseline_mail_outline_24"
app:startIconTint="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/emailAddET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/name"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/full_name_as_per_ic"
app:counterEnabled="true"
app:counterMaxLength="30"
app:boxBackgroundColor="@color/white"
app:boxStrokeColor="@color/black"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/black"
app:startIconDrawable="@drawable/ic_baseline_person_outline_24"
app:startIconTint="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/nameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
app:passwordToggleEnabled="true"
app:boxBackgroundColor="@color/white"
app:boxStrokeColor="@color/black"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/black"
app:startIconDrawable="@drawable/ic_outline_lock_24"
app:startIconTint="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/confirmPassword"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/confirm_password"
app:passwordToggleEnabled="true"
app:boxBackgroundColor="@color/white"
app:boxStrokeColor="@color/black"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/black"
app:startIconDrawable="@drawable/ic_outline_lock_24"
app:startIconTint="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/confirmPasswordET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/goToSignUp2Btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="145dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal|center"
android:background="@color/actionBarColor"
android:contentDescription="@string/next"
android:gravity="center"
android:text="@string/next"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="30dp"
android:onClick="call2ndSignUpScreen"/>
<Button
android:id="@+id/regToSignInBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="@string/already_have_an_account_sign_in_now"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>
</ScrollView>
在您的代码中,您正在使用OnClickListener
regSignUp2Btn.setOnClickListener{
/* ....*/
Intent intent = new Intent(signUpScreen.this, signUpScreen2.class);
//...
startActivity(intent);
}
该侦听器不提供验证,并覆盖布局中使用的android:onclick=“call2ndSignupScreen”
。
更改为:
regSignUp2Btn.setOnClickListener{
if (!validateEmail() | !validateName() | !validatePassword() | !validateConfirmPassword()) {
//setError
return;
}
//....your code
}