Android:更新 UI 的循环中的时间延迟

发布于 2024-12-26 10:58:44 字数 229 浏览 2 评论 0原文

我想要创建的是一个简单的进度条,加载时间约为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

双手揣兜 2025-01-02 10:58:44

使用 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.

彼岸花ソ最美的依靠 2025-01-02 10:58:44

以下代码可能对您有帮助。

public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int value = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setProgress(value);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();
}

The following code may be helpful for you.

public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int value = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setProgress(value);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();
}
三岁铭 2025-01-02 10:58:44

无需显式创建新线程的较短解决方案如下所示:

final Handler handler = new Handler(Looper.getMainLooper());
final int sleepMs = 100;
for (int i = 1; i <= 100; ++i) {
    handler.postDelayed(() -> progressDialog.setProgress(value), i * sleepMs);
}

A shorter solution without explicitly creating a new Thread would look like this:

final Handler handler = new Handler(Looper.getMainLooper());
final int sleepMs = 100;
for (int i = 1; i <= 100; ++i) {
    handler.postDelayed(() -> progressDialog.setProgress(value), i * sleepMs);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文