2

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

更清楚地说,ProgressDialog 正常启动。如何在 n 秒后在其中显示一个按钮?

如果

编辑

使用 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();
4

3 回答 3

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();
     }
  };
}
于 2013-10-14T11:04:35.430 回答
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();
于 2013-10-14T11:23:01.667 回答
0

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

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

于 2013-10-14T11:01:33.803 回答