溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android應(yīng)用開(kāi)發(fā):實(shí)時(shí)改變TextView的值

發(fā)布時(shí)間:2020-07-31 10:06:15 來(lái)源:網(wǎng)絡(luò) 閱讀:3610 作者:wxmgcs 欄目:移動(dòng)開(kāi)發(fā)

線程實(shí)時(shí)刷新TextView值:

思路:

1)使用handler通知view修改值;

2)標(biāo)志位控制線程的停止/開(kāi)始;

android應(yīng)用開(kāi)發(fā):實(shí)時(shí)改變TextView的值android應(yīng)用開(kāi)發(fā):實(shí)時(shí)改變TextView的值


/**

* 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)程


}



附件:http://down.51cto.com/data/2364203
向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI