您好,登錄后才能下訂單哦!
線程實(shí)時(shí)刷新TextView值:
思路:
1)使用handler通知view修改值;
2)標(biāo)志位控制線程的停止/開(kāi)始;
/**
* 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í)時(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++));
}
};
備注:接收消息,并修改停止/開(kāi)始標(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();// 開(kāi)始進(jìn)程
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。