如何在设定时间后关闭 ProgressDialog?
How can I close a ProgressDialog after a set time?
我试图在 3 秒后自动关闭 ProgressDialog 框。这是对话框:
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Connecting");
progress.setMessage("Please wait while we connect to devices...");
progress.show();
我已经尝试了几种方法,但我无法让其中任何一种起作用。希望一个简单的时间或任何东西就足够了。谢谢
AsyncTask 如果您正在处理一个很长的 运行 任务然后想在之后取消它,那么 AsyncTask 是可以的,但是等待 3 秒就有点过分了。尝试一个简单的处理程序。
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Connecting");
progress.setMessage("Please wait while we connect to devices...");
progress.show();
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
progress.cancel();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
已更新添加 show/hide:
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
theLayout.setVisibility(View.GONE);
}
});
我试图在 3 秒后自动关闭 ProgressDialog 框。这是对话框:
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Connecting");
progress.setMessage("Please wait while we connect to devices...");
progress.show();
我已经尝试了几种方法,但我无法让其中任何一种起作用。希望一个简单的时间或任何东西就足够了。谢谢
AsyncTask 如果您正在处理一个很长的 运行 任务然后想在之后取消它,那么 AsyncTask 是可以的,但是等待 3 秒就有点过分了。尝试一个简单的处理程序。
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Connecting");
progress.setMessage("Please wait while we connect to devices...");
progress.show();
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
progress.cancel();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
已更新添加 show/hide:
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
theLayout.setVisibility(View.GONE);
}
});