Problem
I'm trying to validate certain data contained in a% custom View
in the event of clicking the Save button. So after the validation I save the data and close the AlertDialog, and in case the data is not valid I would show the errors to be corrected. But even if I do not call the method AlertDialog
the dialog.dismiss();
in question is closed.
Question
How could I intercept and prevent automatic closing of
AlertDialog
by clicking any of its buttons?
Implementation Attempt
What I'm implementing that is not working is as follows:
AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
dialog.setTitle("Preencha o formulário:");
dialog.setCancelable(true);
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_dialog_forms, null);
dialog.setView(view);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.btn_save), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(validForm()){
save();
dialog.dismiss();
} else {
exibirErrosFormulario();
// ... como não chamo "dialog.dismiss();" não era para fechar o AlertDialog, mas está fechando.
}
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.btn_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});