代码之家  ›  专栏  ›  技术社区  ›  Jose Vega

如何从模态对话中获得响应?

  •  2
  • Jose Vega  · 技术社区  · 15 年前

    我现在有这个:

    Builder yesandno = new AlertDialog.Builder(this);           
    yesandno.setTitle("QuickResponse");
    yesandno.setMessage(message);
    yesandno.setPositiveButton("YES", null);
    yesandno.setNegativeButton("NO", null);
    yesandno.show();
    

    如果用户单击“是”或“否”,如何设置将捕获的事件侦听器?

    2 回复  |  直到 15 年前
        1
  •  6
  •   David Webb    15 年前

    当你打电话 setPositiveButton() setNegativeButton() 而不是通过 null 你应该通过 DialogInterface.OnClickListener .

    例如:

    yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //User clicked yes!
        }
    });
    
        2
  •  4
  •   Erich Douglass    15 年前

    只需做如下的事情:

    yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked yes
        }
    });
    yesandno.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked no
        }
    });
    

    在按钮回调中做任何你想做的。