How to show Progress Dialog Button dynamically after n seconds delay

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:07:33

问题


How to show the ProgressDialog button after n seconds (fixed) delay that the ProgressDialog is shown?

To be more clear, the ProgressDialog starts normally. How can I show a button within it after n seconds?

L.F.

EDIT

Using the answers of Segi and android_beginner (thank you very much) I'm posting the solution of my problem:

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

回答1:


Try Something Like Below Code:

 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:


Just extend this class instead of AsyncTask and be sure to call super() when appropriate:

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



回答3:


make a layout with your ProgressDialog and an invisible button on it. When you show the dialog activate a timer, that set the button visible after n seconds.

I dont know if i understand your question right, but i hope so. =)



来源:https://stackoverflow.com/questions/19358572/how-to-show-progress-dialog-button-dynamically-after-n-seconds-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!