第一步:定义一个Button,id=btnThreadUIPost,定义一个TextView ,id=tvThreadUIPostCount两个控件
第二步:执行Button事件,代码如下:
btnThreadUIPost.setOnClickListener(new OnClickListener(){
new Thread(new Runnable(){
public void run() {
int i=0;
while(i<100)
{
SystemClock.sleep(250);
i++;
final int current=i;
if(i%5==0)
{
tvUIPostCount.post(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
tvUIPostCount.setText(String.valueOf(current)+"% Complete!");
}
});
}
}
final int curCount=i;
tvUIPostCount.post(new Runnable(){
public void run() {
tvUIPostCount.setText(String.valueOf(curCount)+"% Complete!");
}
});
}
}).start();
});