【问题标题】:How to access DIalog's positive and negative buttons in Android如何在 Android 中访问 DIAlog 的正负按钮
【发布时间】:2013-03-07 05:43:31
【问题描述】:

我想在Android中访问Dialog的正负按钮。为什么因为我想限制我的对话的多次可见性。这就是为什么我需要访问这些按钮。但我不知道如何访问。我研究过,但结果仅适用于 alertdialog,不适用于对话。

Dialog mDialog = new DatePickerDialog(IncomeActivity.this,
                 mDatesetListener, calender.get(Calendar.YEAR),
             calender.get(Calendar.MONTH), calender
             .get(Calendar.DAY_OF_MONTH));
mDialog.show();  

我已经尝试了以下代码。但这对我没有帮助。

mDialog.setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface arg0) {
                                isDateClicked = false;
        }
});

isDateClicked 是一个 boolean 值。在打开 mDIAlog 之前,我说它为 true。现在我想在关闭对话框时将其设置为 false。 如何访问这个 mDialog 的正负按钮。

【问题讨论】:

标签: android


【解决方案1】:

您好,您可以使用警报对话框,这样您就可以管理正面和负面按钮,试试下面的代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
 builder.setIcon(android.R.drawable.ic_dialog_info);
 builder.setTitle("Alert dialog title");
 builder.setMessage("Dialog message");
 builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface arg0, int arg1) {
    //DO TASK
  }
  });
 builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface arg0, int arg1) {
    //DO TASK
 }
});

AlertDialog dialog = builder.create();
dialog.show();

【讨论】:

  • 我只需要对话框而不是 alertdialog。
【解决方案2】:

正如 vmironov 所说,我找到了答案。非常感谢 vmironov。答案是

mDialog.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface arg0) {
            isDateClicked = no;
        }
});

【讨论】:

    【解决方案3】:

    您可以使用 mDialog.isShowing() 来了解对话框是否已经启动。

    【讨论】:

      【解决方案4】:

      您可以在对话框中点击 okcancel 按钮。

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle("title");
          builder.setIcon(R.drawable.launcher);
          builder.setMessage("Are you sure you want to exit?")
                 .setCancelable(false)
                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                          Display_questions.this.finish(); // using this current activity is closed
                               // Do task here
      
                     }
                 })
                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                          dialog.cancel();   // using this dialog box is closed.
                     }
                 });
          AlertDialog alert = builder.create();
           alert.show();
      

      祝你好运。

      【讨论】:

        猜你喜欢
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 2012-06-07
        • 2011-05-16
        • 1970-01-01
        • 2012-07-12
        相关资源
        最近更新 更多