如何点击'确定'上AlertDialog通过code? [英] How to click 'OK' on an AlertDialog via code?

查看:181
本文介绍了如何点击'确定'上AlertDialog通过code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的ShowDialog dismissDialog 在活动展示和毁灭我的对话框。有没有也是一种方式发出当前出现的对话框中点击命令不保持一个变量引用对话框?

I use showDialog and dismissDialog in activity to display and destroy my dialog. Is there also a way to issue a click command on the currently displayed dialog without keeping a variable referencing the dialog?

例如,我想preSS通过code中的对话框的确定/正面按钮。

For example, I want to press the 'Ok' / positive button of the dialog via code.

推荐答案

我没有测试此code,但它应该工作:

I haven't tested this code but it should work:

AlertDialog dialog = ...
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();

另外,如果你不想保持对话的引用,但在其设置的控制,你可以提取上点击code到另一种方法:

Alternatively, if you don't want to keep a reference to the dialog but are in control of its setup, you could extract the on click code into another method:

AlertDialog.Builder builder = ...
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {
    onPositiveButtonClicked(); 
  }
});

和实施 onPositiveButtonClicked()在你的活动。而不是编程点击OK按钮,你可以叫 onPositiveButtonClicked() dismissDialog(ID)。如果你需要处理多个对话,有 onPositiveButtonClicked 采取 ID 参数。

and implement onPositiveButtonClicked() in your Activity. Instead of programatically clicking the OK button you can call onPositiveButtonClicked() and dismissDialog(id). If you need to handle multiple dialogs, have onPositiveButtonClicked take an id parameter.

这篇关于如何点击'确定'上AlertDialog通过code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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