Android:更新 UI 的循环中的时间延迟
我想要创建的是一个简单的进度条,加载时间约为 10 秒。 所以我想要的是这样的 for 循环:
for(int i = 1; i <= 100; i++) {
progressDialog.setProgress(i);
//100ms delay
}
谢谢
What I'm trying to create is a simple progress bar, that would load for ~10 sec.
So what I want is a for loop like this:
for(int i = 1; i <= 100; i++) {
progressDialog.setProgress(i);
//100ms delay
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Async Task
您可以在 preExecute() 方法中
将循环索引初始化为 0;在后台进程睡眠线程10秒,然后调用sendUpdate方法将
postExecute更新进度条中的进度发送到参数中的progress获取。
You can use Async Task for the purpose
in preExecute() method initialize loop index to 0;
in background process sleep thread for 10 seconds, and then call sendUpdate method to send progress
in postExecute update progress bar to progress get in parameter.
以下代码可能对您有帮助。
The following code may be helpful for you.
无需显式创建新线程的较短解决方案如下所示:
A shorter solution without explicitly creating a new Thread would look like this: