線程實(shí)時刷新TextView值:
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供九江網(wǎng)站建設(shè)、九江做網(wǎng)站、九江網(wǎng)站設(shè)計(jì)、九江網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、九江企業(yè)網(wǎng)站模板建站服務(wù),10多年九江做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
思路:
1)使用handler通知view修改值;
2)標(biāo)志位控制線程的停止/開始;
/**
* 1、刷新線程
*
* @author wxm
*
*/
class RefreshThread implements Runnable {
public void run() {
while (isStop) {
try {
handler.sendMessage(handler.obtainMessage());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
備注:sendMessage發(fā)送消息
/**
* 2、實(shí)時刷新界面
*/
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("count--->" + count);
if (count == 5) {
isStop = false;
try {
Thread.sleep(3000);
isStop = true;
count = 0;
new Thread(mRefreshThread).start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
textview.setText(String.valueOf(count++));
}
};
備注:接收消息,并修改停止/開始標(biāo)志位
3、初始化控件
private TextView textview = null; // TextView 控件
private int count = 0;//初始化數(shù)值
private RefreshThread mRefreshThread = null;//刷新線程
private boolean isStop = true;// 線程控制標(biāo)志
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textview);
mRefreshThread = new RefreshThread();
textview.setText(String.valueOf(count));
new Thread(mRefreshThread).start();// 開始進(jìn)程
}