-6
如何在不凍結Android應用程序的情況下進行倒計時?例如倒數計數10到1。在Android上進行倒計時而不凍結應用程序?
如何在不凍結Android應用程序的情況下進行倒計時?例如倒數計數10到1。在Android上進行倒計時而不凍結應用程序?
從我的問題中可以得出,你很可能會遇到麻煩,因爲你在主(UI)線程中進行某種倒計時。這肯定會導致應用在定時器期間凍結。只需使用一個定時器,做這樣的事情:
//global variables
private int currTime = 10;
private TextView myTimer;//need to initialize this in your layout using findViewById, or programmatically
final Timer t = new Timer();
currTime = 10;
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {//You need this, or a Handler to ensure the UI is changed on the correct thread.
currTime--;
myTimer.setText(currTime);//myTimer is a global reference to your TextView.
if (currTime == 0)
t.cancel();
});
}
}, 10000);//note that this is the duration in milliseconds (1/1000 of a second) - so 10000 for 10 seconds.
與Java和Android是可能:-) –
看看它http://developer.android.com/reference/android/os/CountDownTimer .html –
我們需要看你的嘗試 – Goaler444