我在活动中使用showDialog
和dismissDialog
来显示和销毁我的对话框。是否还有一种方法可以在当前显示的对话框上发出点击命令,而不需要保留引用该对话框的变量?如何通过代码点击AlertDialog上的“确定”?
例如,我想通过代码按下对话框中的“确定”/肯定按钮。另外
AlertDialog dialog = ...
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
,如果你不希望保留到对话框的参考,但:
AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
alBuilder
.setMessage("Do you wamt to exit?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
// Write your code here for Yes
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
// Write your code here for No
}
});
alBuilder.setCancelable(false);
alBuilder.show();
您是否使用生成器来设置对话框? – TronicZomB 2013-05-07 02:54:31
@TronicZomB:是的,我正在使用'Builder',并且我正在重写'Activity'中的'onCreateDialog'来设置我的对话框。 – Arci 2013-05-07 03:05:42
好吧,bakriOnFire在我注意到他的答案之前就已经有了一个很好的例子,我正准备发布。这几乎是你想要的代码模型。 – TronicZomB 2013-05-07 03:07:31