我有这个用于制作对话框的代码:
Dialog d = new Dialog(this);
d.setTitle("AA");
TextView tv=new TextView(this);
tv.setText("BB");
d.setContentView(tv);
d.show();
我正在寻找将 2 个按钮添加到此对话框并捕获按钮按下事件的任何方法?
谢谢
我有这个用于制作对话框的代码:
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();