Android的循环更新的TextView [英] Android loop update TextView

查看:773
本文介绍了Android的循环更新的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个循环,将更新一个TextView。
我们的想法是建立某种形式的进度指标,这将增加
装载precentge。

I am trying to create a loop that will update a TextView. The idea is to create some sort of progress indicator, that will increment the loading precentge.

这是我已经尽力了,但结果是,我只看到了循​​环的最后一次更新,所以我得到100%,没有incremntation。

This is what I have tried, but the result is that I see only the last update of the loop, so I get "100%" with no incremntation.

    runOnUiThread(new Runnable() {
                         public void run() {
                            final TextView progesss = (TextView)findViewById(R.id.progress);
                            for(int k=1 ; k<=100; k++)
                            {
                                progesss.setText(String.valueOf(k) + "%");

                                try {
                                    Thread.sleep(15);
                                    } catch(InterruptedException e) {
                                    } 
                            }
                         }
                     });

如何实现我的目标任何想法?
谢谢!

Any ideas of how to achieve my goal? Thanks!

推荐答案

的Runnable 块的 UI线程的时做视频下载。相反睡觉,你应该再次发布了新的 Runnable接口。像这样尝试:

Your Runnable blocks the UI thread when doing Thread.sleep. Instead of sleeping, you should post a new Runnable again. Try with something like this:

final Handler handler = new Handler();
handler.post( new Runnable(){ 
    private int k = 0;

    public void run() {
        final TextView progess = (TextView)findViewById(R.id.progress);
        progess.setText(String.valueOf(k) + "%");

        k++;
        if( k <= 100 )
        {
            // Here `this` refers to the anonymous `Runnable`
            handler.postDelayed(this, 15);
        }
    }
});

这将使的 UI线程的一个机会,每次调用之间运行,让它做它的东西一样处理输入和屏幕上绘制的东西。

That will give the UI thread a chance to run between each call, letting it do its stuff like handling input and drawing stuff on the screen.

这篇关于Android的循环更新的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆