4

我创建了一个通用的 OkCancelDialog 类,它可以通过静态方法在我的应用程序中方便地调用:

  static public void Prompt(String title, String message) {
    OkCancelDialog okcancelDialog = new OkCancelDialog();
    okcancelDialog.showAlert(title, message);     
  }

由于各种原因,我需要活动中的 onClick 侦听器,因此在活动中我有:

  public void onClick(DialogInterface v, int buttonId) {
    if (buttonId == DialogInterface.BUTTON_POSITIVE) { // OK button
         // do the OK thing
    }
    else if (buttonId == DialogInterface.BUTTON_NEGATIVE) { // CANCEL button
         // do the Cancel thing
    }
    else {
      // should never happen
    }
  }

这适用于应用程序中的单个对话框,但现在我想添加另一个由同一活动处理的确定/取消对话框。据我所知,只能onClick()为活动定义一个,所以我不确定如何实现它。

有什么建议或提示吗?

4

2 回答 2

6

尝试这样的事情......

public class MyActivity extends Activity
    implements DialogInterface.OnClickListener {

    // Declare dialogs as Activity members
    AlertDialog dialogX = null;
    AlertDialog dialogY = null;

    ...

    @Override
    public void onClick(DialogInterface dialog, int which) {

        if (dialogX != null) {
            if (dialogX.equals(dialog)) {
                // Process result for dialogX
            }
        }

        if (dialogY != null) {
            if (dialogY.equals(dialog)) {
                // Process result for dialogY
            }
        }
    }
}

编辑这就是我创建我的AlertDialogs...

private void createGuideViewChooserDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select Guide View")
        .setSingleChoiceItems(guideViewChooserDialogItems, currentGuideView, this)
        .setPositiveButton("OK", this)
        .setNegativeButton("Cancel", this);
    guideViewChooserDialog = builder.create();
    guideViewChooserDialog.show();
}
于 2012-05-18T20:45:14.053 回答
1

你有两个选择。1)您可以在对话框本身上设置 OnClickListener。2)您可以根据传入的 DialogInterface 来判断哪个对话框是哪个对话框。

于 2012-05-18T20:44:17.030 回答