如何确定ProgressBar何时结束?
问题描述:
我有一个运行10秒的ProgressBar
。我希望我的应用在进度条结束时显示AlertDialog
。 如何操作?如何确定ProgressBar何时结束?
做while
周期与sleep()
不适合我。也许我使用的方式不好?
while (progressBar.getProgress() != progressBar.getMax()) {
sleep(1000);
}
// My AlertDialog will be displayed here
如果有快速和肮脏的办法做到这一点,我会更喜欢它。我正在编程的应用程序相当小。
答
它是您的程序,它根据任务的进度设置progressBar的进度,并知道任务何时结束。然后你可以关闭progressBar来关闭它。当你关闭progressBar时,你可以显示改变对话框。
看看这个例子 - https://www.mkyong.com/android/android-progress-bar-example/。只需在以下语句后显示您的警报对话框。
// close the progress bar dialog
progressBar.dismiss();
答
使用Handler .. while或sleep会冻结其他进程。
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
},10000);