android 计时器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;pub
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));
}
}
}
更多推荐
所有评论(0)