Android 安卓:想咨询一下对话框添加按钮的问题吗

Android 安卓:想咨询一下对话框添加按钮的问题吗,android,Android,我有以下代码用于创建对话框: Dialog d = new Dialog(this); d.setTitle("AA"); TextView tv=new TextView(this); tv.setText("BB"); d.setContentView(tv); d.show(); 我正在寻找任何方法添加2个按钮到这个对话框和捕捉按钮按下事件 谢谢我会使用AlertDialog.Builder而不是Dialog构造函数: AlertDilog ad = new AlertDialog.Bu

我有以下代码用于创建对话框:

Dialog d = new Dialog(this);
d.setTitle("AA");
TextView tv=new TextView(this);
tv.setText("BB");
d.setContentView(tv);
d.show();
我正在寻找任何方法添加2个按钮到这个对话框和捕捉按钮按下事件


谢谢

我会使用
AlertDialog.Builder
而不是
Dialog
构造函数:

AlertDilog ad = new AlertDialog.Builder(this).create();
ad.setTitle(R.string.app_name);
ad.setMessage(this.getString(R.string.dialog_message);
ad.setCancelable(true);

ad.setButton(AlertDialog.BUTTON1, this.getString(R.string.first_btn_label), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //Do something when the first button is pressed
        dismissDialog(DIALOG_SOLVED);
    }
}); 

ad.setButton(AlertDialog.BUTTON2, this.getString(R.string.second_btn_label), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //Do something when the second button is pressed
        dismissDialog(DIALOG_SOLVED);
    }
}); 

//You can even add a third button if you want to
/*
ad.setButton(AlertDialog.BUTTON3, this.getString(R.string.third_btn_label), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //Do something when the third button is pressed
        dismissDialog(DIALOG_SOLVED);
    }
}); 
*/

ad.show();
这样地?