4
Problem
I’m trying to validate certain data contained in a View
customized of a AlertDialog
in the Save button click event. Then after the validation I save the data and close the Alertdialog, and in case the data are not valid I would show the errors to be corrected. But even if I don’t call the method dialog.dismiss();
the AlertDialog
in question is closed.
Question
How could I intercept and prevent the automatic closing of the
AlertDialog
by clicking any of its buttons?
Attempt at implementation
What I’m implementing that’s not working is the following:
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();
}
});
Another alternative is http://answall.com/a/85735/5801.
– Geison Santos
@Geisonsantos, which in the end is the same thing, only taking a few more turns, because the secret of the solution is to overwrite the event of clicking the patterns buttons
Dialog
, like the @ramaral, indicates in his reply.– Fernando Leal
The difference is that I write less, @Fernando. In the end the result is really, yes. It’s more a matter of taste.
– Geison Santos