带有按钮onClick事件的android自定义对话框 [英] android custom Dialog with Button onClick event

查看:86
本文介绍了带有按钮onClick事件的android自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的CustomDialog.java:

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

}

这是Activity中使用CustomDialog的代码:

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

我要这样:

1.在Activity1中,当单击ImageButton时,将显示CustomDialog。

2.显示CustomDialog后,如果单击Button是,它将对数据做一些操作,然后转到Activity2。

3.如果单击Button否,CustomDialog将关闭并且不对数据做任何操作。

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.

但是我的问题是:

,当单击ImageButton时,CustomDialog显示,我想对数据执行的代码将执行并自动转到Activity2。

我无法选择是或否来单击。

我认为Button中的问题是onClick事件。

如何解决?

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?

推荐答案


参数initilize



  Dialog dialog;




在您要使用的地方使用此功能



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

这篇关于带有按钮onClick事件的android自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆