Android忘记密码弹窗空邮箱字段处理:避免返回登录页问题
Hey there, let's fix those two issues step by step—keeping the dialog open when the email is empty, and adding proper validation for the email field.
Why the current code closes the dialog
Right now, when you click the "Yes" button, the AlertDialog automatically closes after your onClick logic runs—even if you just show a Toast and return. That's why you're getting sent back to the Login Activity. We need to take control of when the dialog closes.
Solution 1: Keep the dialog open on invalid input
Instead of using setPositiveButton() directly (which triggers the dialog to close by default), we'll create the dialog first, then get a reference to the positive button and attach our custom click listener. This way, we can decide whether to close the dialog only when validation passes.
Solution 2: Add proper email validation
We'll not only check if the field is empty but also verify that the input matches a valid email format using Patterns.EMAIL_ADDRESS from the Android framework.
Modified Code
forgetPass.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final EditText resetMail = new EditText(v.getContext()); final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext()); passwordResetDialog.setTitle("Reset Password?"); passwordResetDialog.setMessage("Enter Your Email To Receive The Reset Link"); passwordResetDialog.setView(resetMail); // Create the dialog first, don't show it yet final AlertDialog dialog = passwordResetDialog.create(); // Set positive button placeholder to avoid default behavior dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { // Empty listener to override default close behavior } }); // Attach custom logic to the positive button after dialog is shown dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); positiveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String mail = resetMail.getText().toString().trim(); // Validation 1: Check if email field is empty if (TextUtils.isEmpty(mail)) { Toast.makeText(getApplication(), "Enter Valid Email Address.", Toast.LENGTH_SHORT).show(); // Keep dialog open, don't dismiss return; } // Validation 2: Check if email format is valid if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()) { Toast.makeText(getApplication(), "Please enter a valid email format.", Toast.LENGTH_SHORT).show(); // Keep dialog open for correction return; } // Proceed with password reset only if validation passes fAuth.sendPasswordResetEmail(mail) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show(); // Close dialog now that operation is successful dialog.dismiss(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(Login.this, "Error ! Reset Link Not Sent." + e.getMessage(), Toast.LENGTH_SHORT).show(); // Keep dialog open so user can retry or correct input } }); } }); } }); // Set negative button to close dialog normally dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { dialog.dismiss(); } }); dialog.show(); } });
Key Changes Explained
- Controlled Dialog Closure: By using
setOnShowListenerto attach a custom click handler to the positive button, we avoid the default auto-close behavior. The dialog only closes when the email is valid and the reset link is sent successfully. - Enhanced Validation: We added a check for valid email structure using Android's built-in
Patterns.EMAIL_ADDRESS, which ensures the input follows standard email formatting rules. - User-Friendly Error Handling: Now, if the email is empty or invalid, the dialog stays open, letting the user correct their input without being sent back to the login screen.
内容的提问来源于stack exchange,提问作者Chaitya Vohera

