0

当我使用警报对话框时,我可以使用以下方法添加一个“取消”按钮:

AlertDialog.Builder builder = new Builder(getActivity());
builder.setTitle("Students").setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog,int id) {
    // if this button is clicked, just close
    // the dialog box and do nothing
    dialog.cancel();
  }
});;

现在我正在实现我自己的片段。如何设置相同的取消按钮?

4

1 回答 1

0

您可以简单地覆盖DialogFragmentonCreateDialog()方法:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new Builder(getActivity());
builder.setTitle("Students").setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog,int id) {
    // if this button is clicked, just close
    // the dialog box and do nothing
    dialog.cancel();
  }
});;
        return builder.create();
    }

希望这可以帮助。

于 2013-01-17T16:04:04.520 回答