Find Parent Button Dialog

I have a warning dialog box containing a button. The button is defined in some XML, and the layout is set using Dialog.setContentView ().

The button has a listener class, and I want to know how, if at all, I can access the dialog from the onClick (View v) method.

The reason for this is simply that I want to remove the dialogue, so if there is an easier / better way to do this, then it will be useful to know!

+3
source share
3 answers

A simple solution using the method onCreateDialog()from the class Activity:

// member variable
Dialog mDialog;

protected Dialog onCreateDialog(int id) {
    Builder builder = new AlertDialog.Builder(mContext);
    switch (id) {
        case DELETE_ALL_DIALOG:
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.ok, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do what you want
                }
            });
            builder.setNegativeButton(R.string.cancel, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismissDialog(DELETE_ALL_DIALOG); // thats what you are looking for
                }
            });
            builder.setMessage(R.string.delete_all_bookmarks_question);
            mDialog = builder.create();
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            return mDialog;
        default:
            return super.onCreateDialog(id);
    }
}
+1
source

Create your own dialog class and define it. Sort of:

public class CustomizeDialog extends Dialog implements OnClickListener {
    Button okButton;

    public CustomizeDialog(Context context) {
        super(context);
        /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /** Design the dialog in main.xml file */
        setContentView(R.layout.main);
        okButton = (Button) findViewById(R.id.OkButton);
        okButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        /** When OK Button is clicked, dismiss the dialog */
        if (v == okButton)
            dismiss();
    }
0

, () .

This doesn't seem like the best way - is there a better one?

Creating a custom dialog seems unnecessary to me ... It does not require anything unusual.

0
source

Source: https://habr.com/ru/post/1762820/


All Articles