进度对话框不关闭

Progress Dialog not dismissing

在下面的代码中显示了延迟 5 秒的进度对话框。 5 秒后想要关闭对话框,但在下面没有发生。

谁能帮我看看我错在哪里?

new CountDownTimer(5000, 1000) 
{
    public void onTick(long millisUntilFinished) 
    {
       // You don't need anything here
       dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
    }

    public void onFinish() 
    {
       dialog.dismiss();
    }

 }.start();

而不是将进度对话框的值分配给对话框变量。创建进度对话框 object 并如下设置标题。

ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("what ever message you like");
pd.show();

onFinish()请致电pd.dismiss();

如果尚未显示进度对话框,请在 onTick 方法中设置消息并在此方法中显示进度对话框。

ProgressDialog pd = new ProgressDialog(yourActivity.this);
new CountDownTimer(5000, 1000) 
{
    public void onTick(long millisUntilFinished) 
    {
       // You don't need anything here
       pd.setMessage("what ever message you like");
        if (!pd.isShowing())
            pd.show();
    }

    public void onFinish() 
    {
       pd.dismiss();
    }

 }.start();

on tick 多次调用,这就是您的对话框未关闭的原因

实现这个

new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
                // You don't need anything here
                if(!dialog.isShowing())
                {
                    dialog= ProgressDialog.show(mContext,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
                }
            }
            public void onFinish() {
                dialog.dismiss();
            }
        }.start();

您正在呼叫

dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);

在方onTick()方法中。

它将在每次更新时创建新的进度对话框实例。这可能会导致您的功能出现问题,并且在这种情况下您的对话框不会关闭。

请尝试使用以下代码

final ProgressDialog dialog = new ProgressDialog(DynamicView.this);
dialog.setTitle("Delay");
dialog.setMessage("Please wait...." + 5000 / 1000 + " Second(s)");
dialog.setIndeterminate(true);

new CountDownTimer(5000, 1000) {

    public void onTick(long millisUntilFinished) {
        // You don't need anything here
        dialog.setMessage("Please wait...." + millisUntilFinished / 1000 + " Second(s)");

        if (!dialog.isShowing())
            dialog.show();
    }

    public void onFinish() {
        if (dialog.isShowing())
            dialog.dismiss();
    }
}.start();

此代码将按您的需要工作。