进度对话框未关闭

时间:2019-01-04 08:48:19

标签: android progressdialog

在下面的代码中显示进度对话框,延迟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();

3 个答案:

答案 0 :(得分:1)

您正在呼叫

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();

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

答案 1 :(得分:0)

不是将“进度对话框”的值分配给对话框变量。创建进度对话框对象,并为其设置标题,如下所示。

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();

答案 2 :(得分:0)

关于滴答声正在调用多个时间,这就是对话框未关闭的原因

实现此

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();