![](/img/trans.png)
[英]I have button when I click on that button I want to show progress dialog only for 2 seconds, how to dismiss progress dialog after 2 seconds
[英]How to show Progress Dialog Button dynamically after n seconds delay
在显示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();
只需扩展此类而不是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();
}
};
}
试试下面的代码:
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();
使用您的ProgressDialog和一个不可见的按钮进行布局。 当您显示对话框时,激活计时器,该按钮会将按钮设置为在n秒后可见。
我不知道我是否理解您的问题,但我希望如此。 =)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.