[英]Error Messages not showing inside Alert Dialog Box
我想在密碼重置的警報對話框中顯示錯誤消息,但是在彈出的對話框中沒有顯示錯誤消息。
單擊“重置”時,對話框將關閉。 但是,輸入有效的 email 確實會顯示吐司消息“重置 Email 已發送”
驗證是 1) 將 email 地址留空,2) 沒有給出正確的 email
登錄.java
//onclick for forgot password
forgotPText = findViewById(R.id.forgotPassText);
forgotPText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start alert dialog
View view = inflater.inflate(R.layout.reset_popup,null);
reset_alert.setTitle("Forgot Password ?")
.setMessage("Enter Email For Password Reset Link.")
.setPositiveButton("Reset", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//validate the email address is not empty or not valid email
EditText email = view.findViewById(R.id.reset_popup_Email);
String emailChar = email.getText().toString();
if (email.getText().toString().isEmpty()){
email.setError("Required Field!");
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(emailChar).matches()){
email.setError("Please provide valid email!");
email.requestFocus();
return;
}
//send reset link
firebaseAuth.sendPasswordResetEmail(email.getText().toString())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//FirebaseUser user = firebaseAuth.getCurrentUser();
//updateUI(user);
Toast.makeText(LoginActivity.this
, "Reset Email Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginActivity.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
}).setNegativeButton("Cancel",null)
.setView(view)
.create().show();
}
});
使用此代碼:
forgotPText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start alert dialog
LayoutInflater inflater = getLayoutInflater();
AlertDialog.Builder reset_alert = new AlertDialog.Builder(MainActivity.this);
View view = inflater.inflate(R.layout.reset_popup,null);
reset_alert.setView(view);
EditText email = view.findViewById(R.id.email);
TextView tv_cancel = view.findViewById(R.id.tv_cancel);
TextView tv_submit = view.findViewById(R.id.tv_submit);
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
new Handler().postDelayed(new Runnable() {
@Override
public void run() { alertDialog.dismiss(); }}, 200);
}
});
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
tv_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
String emailChar = email.getText().toString();
if (email.getText().toString().isEmpty()){
email.setError("Required Field!");
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(emailChar).matches()){
email.setError("Please provide valid email!");
email.requestFocus();
return;
}
//send reset link
firebaseAuth.sendPasswordResetEmail(email.getText().toString())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//FirebaseUser user = firebaseAuth.getCurrentUser();
//updateUI(user);
Toast.makeText(LoginActivity.this
, "Reset Email Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginActivity.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
});
alertDialog = reset_alert.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
});
//////// reset_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/reset_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Forgot Password?"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="@color/black"
android:text="Enter Email For Password Reset Link."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/reset_alert" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Email Address"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/des" />
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Cancel"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tv_submit"
app:layout_constraintTop_toBottomOf="@+id/email" />
<TextView
android:id="@+id/tv_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginEnd="353dp"
android:text="Reset"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />
</androidx.constraintlayout.widget.ConstraintLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.