【问题标题】:Android AlertDialog button clickAndroid AlertDialog 按钮单击
【发布时间】:2011-12-14 13:02:18
【问题描述】:

我得到了一个 AlertDialog,每个都有一个正负按钮。在 onClick(DialogInterface dialog, int item) 事件处理程序中,它是 Activity 的成员函数,我如何知道单击了哪个按钮?根据我的观察,如果单击正按钮,“item”参数的值为-1,如果是负按钮,则值为-2。但是,我无法从 API 文档中找到有关此类的任何信息,因此我觉得这可能随时被破坏。

【问题讨论】:

    标签: android


    【解决方案1】:

    AlertDialog 的正面和负面按钮使用 DialogInterface.OnClickListener 接口来响应选择。就像你说的,这个回调的 onClick 方法是这样的:

    public void onClick(DialogInterface dialog, int which) {
      // ...
    }
    

    which 参数指示单击了哪个按钮,并且可以具有以下值: 1) AlertDialog.BUTTON_POSITIVE (-1) 2) AlertDialog.BUTTON_NEGATIVE (-2)

    所以,如果是 -1,则单击正按钮,如果是 -2,则单击负按钮。

    【讨论】:

    • 谢谢老兄。我没有注意到静态字段。
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多