简体   繁体   English

带有按钮 onClick 事件的 android 自定义对话框

[英]android custom Dialog with Button onClick event

i have CustomDialog.java like this :我有这样的 CustomDialog.java:

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 :这是 Activity 中使用 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. 1. 在Activity1中,当点击ImageButton时,会显示CustomDialog。
2. after CustomDialog show, if click Button yes, it doing something with data and go to Activity2. 2. CustomDialog 显示后,如果单击 Button 是,它会处理数据并转到 Activity2。
3. if click Button no, CustomDialog will close and don't doing something with data. 3.如果单击按钮否,CustomDialog 将关闭并且不会对数据执行任何操作。

but my problem is :但我的问题是:
when click ImageButton, CustomDialog show, the code I want to do with data will doing and auto go to Activity2.当单击 ImageButton,CustomDialog 显示时,我想对数据执行的代码将执行并自动转到 Activity2。
I can't choose yes or no to click.我不能选择是或否来点击。
I think problem in Button yes onClick event.我认为 Button 中的问题是 onClick 事件。
how to fix it?如何解决?

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();
    }
});

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.您的代码看起来不错,但对于onClickListeners你的按钮,我不明白为什么你还是要调用CheckYes()时,你可以做任何你想在做onClick方法。

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?请准确说明“单击 YES 按钮时对数据执行某些操作”的含义 - 当您说您不能选择是或否来单击时,您是什么意思?

For me I had to make sure that the dialog was created before accessing using findViewById on the dialog.对我来说,我必须确保在对话框上使用 findViewById 访问之前创建了对话框。 Inside the inner class that I made the dialog in, in the OnStart callback function is where I accessed the buttons.在我创建对话框的内部类中,在 OnStart 回调函数中是我访问按钮的地方。 I tried doing it outside the class when I said .show() but it was giving me a null pointer exception.当我说 .show() 时,我尝试在课外这样做,但它给了我一个空指针异常。

For me I the only way I could access buttons iD's in the dialog was through the dialog.对我来说,我可以访问对话框中按钮 iD 的唯一方法是通过对话框。 I couldn't just do getActivity().findViewById(R.id.blah)我不能只做 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();
                }
            });

        }
    txt_showdailog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Dialog dialog =new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.bluetooothdialog);
            dialog.show();


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM