android custom Dialog with Button onClick event

2019-06-20 07:18发布

i have CustomDialog.java like this :

public class CustomDialog {
Dialog dl;
public void ShowDialog(Context context, String message) {
    dl = new Dialog(context);
    dl.setContentView(R.layout.custom_dialog);

    TextView tv_message = (TextView) dl.findViewById(R.id.textViewMessage);

    tv_message.setText(message);

    Button bt_yes = (Button)dl.findViewById(R.id.buttonYes);
    Button bt_no = (Button)dl.findViewById(R.id.buttonNo);

    bt_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckYes();
        }
    });
    bt_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dl.dismiss();
        }
    });
    dl.show();

}

public Boolean CheckYesNo(Boolean check){
    return check;
}

public Boolean CheckYes() {
    return true;
}

public void CloseDialog() {
    dl.dismiss();
}

}

this is code in Activity to use CustomDialog :

CustomDialog cdl = new CustomDialog ();
                        cdl.ShowDialog(Activity1.this, "test");
                        if (cdl.CheckYesNo(true)) {
                            // doing something with data and go to Activity 2
                        }
                        else {
                            cdl.CloseDialog();
                        }

I want like this :
1. In Activity1, when click ImageButton, CustomDialog will show.
2. after CustomDialog show, if click Button yes, it doing something with data and go to Activity2.
3. if click Button no, CustomDialog will close and don't doing something with data.

but my problem is :
when click ImageButton, CustomDialog show, the code I want to do with data will doing and auto go to Activity2.
I can't choose yes or no to click.
I think problem in Button yes onClick event.
how to fix it?

3条回答
太酷不给撩
2楼-- · 2019-06-20 07:56

parameter initilize

  Dialog dialog;

You use this where You want to use

dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.show();


TextView tv_message = (TextView) dialog .findViewById(R.id.textViewMessage);

tv_message.setText(message);

Button bt_yes = (Button)dialog.findViewById(R.id.buttonYes);
   Button bt_no = (Button)dialog.findViewById(R.id.buttonNo);

bt_yes.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        CheckYes();
    }
});
bt_no.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});
查看更多
Summer. ? 凉城
3楼-- · 2019-06-20 08:07

For me I had to make sure that the dialog was created before accessing using findViewById on the dialog. Inside the inner class that I made the dialog in, in the OnStart callback function is where I accessed the buttons. I tried doing it outside the class when I said .show() but it was giving me a null pointer exception.

For me I the only way I could access buttons iD's in the dialog was through the dialog. I couldn't just do getActivity().findViewById(R.id.blah)

Here's the code I used:

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


        if(getDialog() == null){
            return;
        }
        else {

            ImageButton closeButton = (ImageButton)getDialog().findViewById(R.id.closeButton);

            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDialog().dismiss();
                }
            });

        }
查看更多
仙女界的扛把子
4楼-- · 2019-06-20 08:15

Your code looks OK, except that for the onClickListeners of your buttons, I don't see why you still want to call CheckYes() when you can just do whatever you wanted to do in the onClick method.

This is what the code should look like:

public void ShowDialog(final Context context, String message) {
    dl = new Dialog(context);
    dl.setContentView(R.layout.custom_dialog);

    TextView tv_message = (TextView) dl.findViewById(R.id.textViewMessage);

    tv_message.setText(message);

    Button bt_yes = (Button)dl.findViewById(R.id.buttonYes);
    Button bt_no = (Button)dl.findViewById(R.id.buttonNo);

    bt_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //You said you wanted to do something with the data? What data? Anyway, this is where you can do it
            //do whatever with the "data"
            //this is where you want to go to another activity
            Intent activity2 = new Intent(context, <Your-Activity2-Name>.class);
            context.startActivity(activity2);
            //and you still want to dismiss the dialog
            dl.dismiss();
        }
    });
    bt_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dl.dismiss();
        }
    });
    dl.show();

}

I hope this sheds some light. Please clarify exactly what you mean by "doing something with the data when YES button is clicked" - also when you say you can't choose yes or no to click, what do you mean?

查看更多
登录 后发表回答