Android的循环更新的TextView [英] Android loop update 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屋!