6

我有一个像这样实现的 ProgressDialog:

// show progress dialog while date is loading
        progressDialog = ProgressDialog.show(XYActivity.this, getResources().getString(R.string.progress_dialog_please_wait), getResources().getString(R.string.progress_dialog_loading), true);
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                cancel(true);
                Log.w(LOGTAG, "loading cancelled via back button");
            }

        });
        progressDialog.setCancelable(true);

此 ProgressDialog 在 AsyncTask (PreExecute) 内实现,因此 cancel(true) 方法会停止 AsyncTask。这一切都很好。

问题是,我可以通过屏幕上的任何随机触摸来取消ProgressDialog 。我只想通过按后退按钮来关闭对话框。请帮我!谢谢你们。

4

2 回答 2

7

这对我有用:

@Override
protected void onPreExecute() {
    progressDialog = ProgressDialog.show(context, "Title", "Loading...", true, true, new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            cancel(true);
        }
    });
    progressDialog.setCanceledOnTouchOutside(false);
}

GedankenNebelsetCanceledOnTouchOutside建议的非常干净。

于 2013-02-13T06:39:22.620 回答
1

试试下面的说明

不确定整个取消按钮...我听说过 onCancel() 方法未正确触发的报告。我的解决方案只是在对话框上制作一个普通按钮,并在按下按钮时调用返回。

private void createCancelProgressDialog(String title, String message, String buttonText)
{
    cancelDialog = new ProgressDialog(this);
    cancelDialog.setTitle(title);
    cancelDialog.setMessage(message);
    cancelDialog.setButton(buttonText, new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            // Use either finish() or return() to either close the activity or just the dialog
            cancelDialog.dismiss();
        }
    });
    cancelDialog.show();
}

然后只需在活动中的其他地方使用简单的调用方法

createCancelProgressDialog("Loading", "Please wait while activity is loading", "Cancel");

相当简单的解决方案,但它可以解决问题;)还只是要注意 cancelDialog 是一个活动擦除变量,如果您不需要从其他地方调用它,那么您应该能够仅将变量的范围限制为那个方法。

于 2012-04-23T11:34:54.600 回答