Android AlertDialog with DialogFragment: do not close the dialog box even if you click OK

I have an AlertDialog with a custom layout (EditText only) and I want to check the data when I click OK. If the check fails, I do not want to close the dialog.

I use the default dialog buttons (positive and negative). If I use "setPositiveButton (" ", the new DialogInterface.OnClickListener () ..." the dialog is always closed. I saw several messages and they said that the onClick listener should be redefined, but I can not get it to work This is the code that I found:

Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

Since he says that this should be done AFTER the dialog box is shown, I put this code inside my activity and not inside my DialogFragment, but if I use mDialogFragment.getDialog (), it always returns null.

This is part of my dialogue snippet:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.new);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    dialogView = inflater.inflate(R.layout.edit_license, null);

    builder.setView(dialogView)

    // Add action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MyDialogFragment.this.getDialog().cancel();
        }
    }); 

    return builder.create();

}

In my activity, I do the following:

DialogFragment dialog = new MyDialogFragment(true, null);
dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");

AlertDialog alertDialog = (AlertDialog)dialog.getDialog();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setOnClickListener(new CustomListener(alertDialog));

... 

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {
        ...
    }
}

As I said, (AlertDialog) dialog.getDialog (); always returns null. Why is this? How can I avoid closing the dialog if the check does not fit?

Thank!

+5
source share
2 answers

here is some text from the DialogFragment link :

, , . Dialog, . (, , , ) API , .

getDialog().() ()

+3

. onResume, .

public class XYZFragment extends DialogFragment {
   ...

    @Override
    public void onResume() {
        super.onResume();

        AlertDialog dialog = (AlertDialog)getDialog();

        Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {                  
            @Override
            public void onClick(View onClick) {
            ...
            }
        });
    }
}
+4

All Articles