如何在n秒延迟后动态显示进度对话框按钮

时间:2013-10-14 10:49:50

标签: android button delay progressdialog

如何在显示ProgressDialog的n秒(固定)延迟后显示ProgressDialog按钮?

更清楚的是,ProgressDialog正常启动。如何在n秒后显示其中的按钮?

L.F。

修改

使用Segi和android_beginner的答案(非常感谢)我发布了我的问题的解决方案:

        pDialog = new ProgressDialog(mContext);
        pDialog.setTitle(Reso.getString(mContext, R.string.waiting));
        pDialog.setMessage(Reso.getString(mContext, R.string.waiting));
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, 
                Reso.getString(mContext, R.string.annulla), 
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Stuff
            }
        });
        pDialog.show();
        pDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);

        int progressDialogCancelButtonDelay = 2500;

        new CountDownTimer(progressDialogCancelButtonDelay, progressDialogCancelButtonDelay + 1) {

            @Override public void onTick(long millisUntilFinished) { }

            @Override public void onFinish() {
                pDialog.getButton(ProgressDialog.BUTTON_NEGATIVE).setVisibility(View.VISIBLE); 
            }
        }.start();

3 个答案:

答案 0 :(得分:2)

只需扩展此类而不是AsyncTask,并确保在适当时调用super()

public abstract class AsyncTaskWithDelayedIndeterminateProgress
  <Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private static final int MIN_DELAY = 250;
private final ProgressDialog progressDialog;
private final CountDownTimer countDownTimer;

protected AsyncTaskWithDelayedIndeterminateProgress(Activity activity) {
  progressDialog = createProgressDialog(activity);
  countDownTimer = createCountDownTimer();
}

@Override protected void onPreExecute() {
  countDownTimer.start();
}

@Override protected void onPostExecute(Result children) {
  countDownTimer.cancel();
  if(progressDialog.isShowing())
     progressDialog.dismiss();
}

private ProgressDialog createProgressDialog(Activity activity) {
  final ProgressDialog progressDialog = new ProgressDialog(activity);
  progressDialog.setIndeterminate(true);
  return progressDialog;
}

private CountDownTimer createCountDownTimer() {
  return new CountDownTimer(MIN_DELAY, MIN_DELAY + 1) {
     @Override public void onTick(long millisUntilFinished) { }

     @Override public void onFinish() {
        progressDialog.show();
     }
  };
}

答案 1 :(得分:2)

尝试下面的代码:

 ProgressDialog loadingDialog = ProgressDialog.show(activity.this, "", "Loading...",
                true, false);

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Thread.sleep(2500);
                    runOnUiThread(new Runnable() {
                        public void run() {



                                loadingDialog.dismiss();

                               loadingDialog.setButton(ProgressDialog.BUTTON_NEUTRAL, 
                                                 "Close", 
                               new DialogInterface.OnClickListener()                 {                   
                              @Override
                               public void onClick(DialogInterface dialog, int which) {
                            //button click stuff here
                          }
                     });

                    loadingDialog.show(); 
                   loadingDialog.getButton(ProgressDialog.BUTTON_NEUTRAL).setVisibility(View.INVISIBLE); 

                            }
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

答案 2 :(得分:0)

使用ProgressDialog和一个隐形按钮进行布局。 当您显示对话框时,激活一个计时器,在n秒后将按钮设置为可见。

我不知道我是否理解你的问题,但我希望如此。 =)