【问题标题】:Add cancel button on progress dialog在进度对话框上添加取消按钮
【发布时间】:2017-06-14 05:04:36
【问题描述】:

我正在尝试在 Xamarin Android 中启用 ProgressDialog 上的取消按钮,但它没有出现。

这是我到现在为止所做的:

ProgressDialog progressDialog = new ProgressDialog(Context);
progressDialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
progressDialog.SetCancelable(true);

progressDialog.CancelEvent += (o, e) =>
{
    // Cancel download
};

progressDialog.Show();

相关问题:How to set cancel button in Progress Dialog?Android ProgressDialog can't add Cancel button

【问题讨论】:

    标签: android xamarin xamarin.android progressdialog


    【解决方案1】:

    注意:ProgressDialog 现在在 API-26 中为 deprecated

    var progress = new ProgressDialog(this);
    progress.SetTitle("Syncing Events");
    progress.Indeterminate = false;
    progress.SetProgressStyle(ProgressDialogStyle.Horizontal);
    progress.Max = totalEvents;
    progress.Progress = currentEvent;
    progress.SetButton(-3, "CancelLeft", (sender, e) => {
        Log.Debug("SO", "Cancel");
    });
    progress.SetButton(-2, "CancelMiddle", (sender, e) =>
    {
        Log.Debug("SO", "Cancel");
    });
    progress.SetButton(-1, "CancelRight", (sender, e) =>
    {
        Log.Debug("SO", "Cancel");
    });
    progress.Show();
    

    【讨论】:

    • 这是一个更好的答案
    【解决方案2】:

    我设法做到了以下方式:

    progressDialog.SetButton("Cancel", new EventHandler<DialogClickEventArgs>(
        (s, args) => { 
            // Cancel download
        }
    ));
    

    【讨论】:

    • 仅供参考:自 API 3 以来已弃用 SetButton 方法,请使用接受 int (whichButton) 作为第一个参数的 SetButton 方法
    【解决方案3】:
    ProgressDialog myDialog = new ProgressDialog(YourActivity.this);
    myDialog.setMessage("Loading...");
    myDialog.setCancelable(false);
    myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    myDialog.show();
    

    【讨论】:

    • 这是 Java。我问的是 Xamarin/C#。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    相关资源
    最近更新 更多