5
我試圖從http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/創建倒數計時器,但我無法讓它與我想要的00:00:00格式一起工作,如文章中所述。計時器將啓動,並讓我暫停它,它會運行兩分鐘,但你看不到它正在運行。如果你暫停它,你可以看到剩下多少時間,但你卻無法讓它工作。沒有格式化,它可以正常工作並倒計時。有沒有人有這個工作或知道如何解決它?我一直在四處搜尋,找不到像倒數計時器一樣的格式化任何東西。任何幫助,將不勝感激。使用00:00:00格式化倒數計時器不起作用。顯示00:00:00直到暫停,然後顯示剩餘時間
TextView timeDisplay;
MyCount counter;
int state = 0;
int length = 120000;
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
Button control;
public String formatTime(long millis) {
String output = "00:00:00";
long seconds = millis/1000;
long minutes = seconds/60;
long hours = minutes/60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD + " : " + minutesD + " : " + secondsD;
return output;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countdown);
timeDisplay = (TextView) findViewById(R.id.timer);
control = (Button) findViewById(R.id.control);
counter = new MyCount (length, 1000);
}
public void control(View view) {
switch (state) {
case 0:
startTime = System.currentTimeMillis();
counter.start();
control.setText(R.string.pause);
state = 1;
break;
case 1:
// pause
currentTime = System.currentTimeMillis();
timeElapsed = currentTime - startTime;
if (prevTimeRemaining == 0)
timeRemaining = length - timeElapsed;
else
timeRemaining = prevTimeRemaining - timeElapsed;
counter.cancel();
timeDisplay.setText("" + formatTime(timeRemaining));
control.setText(R.string.resume);
prevTimeRemaining = timeRemaining;
// resume
counter = new MyCount(timeRemaining, 1000);
state = 0;
break;
case 2:
prevTimeRemaining = 0;
counter = new MyCount(length, 1000);
control.setText(R.string.start);
timeDisplay.setText(R.string.timer);
state = 0;
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("done!");
state = 2;
control.setText(R.string.restart);
}
public void onTick (long millisUntilFinished) {
timeDisplay.setText ("Left: " + formatTime(timeRemaining));
}
}
}
我明白了。原來是timeDisplay.setText(「Left:」+ formatTime(timeRemaining));是timeDisplay.setText(「Left:」+ millisUntilFinished/100);我曾嘗試將formatTime放在millisUntilFinished前沒有成功,但我試過沒有/ 100,它的工作原理 – Daniel 2010-10-17 01:29:47
然後請 回答並檢查您自己的答案,以便將此問題從未答覆的列表中刪除。 – rds 2011-11-28 20:00:27