線程實(shí)時(shí)刷新TextView值:
成都創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供九江網(wǎng)站建設(shè)、九江做網(wǎng)站、九江網(wǎng)站設(shè)計(jì)、九江網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(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)志位控制線程的停止/開(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)程
}
網(wǎng)站標(biāo)題:android應(yīng)用開(kāi)發(fā):實(shí)時(shí)改變TextView的值
文章地址:http://aaarwkj.com/article10/peisgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、品牌網(wǎng)站制作、全網(wǎng)營(yíng)銷(xiāo)推廣、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)