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));

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐