我開發了一個倒數計時器,我不確定如何暫停和恢復定時器的textview正在點擊。點擊開始,然後再次點擊以暫停並恢復,再次點擊計時器的文本視圖。Android:如何暫停和恢復倒數計時器?
這是我的代碼:
Timer = (TextView) this.findViewById(R.id.time); //TIMER
Timer.setOnClickListener(TimerClickListener);
counter = new MyCount(600000, 1000);
}//end of create
private OnClickListener TimerClickListener = new OnClickListener() {
public void onClick(View v) {
updateTimeTask();
}
private void updateTimeTask() {
if (decision == 0) {
counter.start();
decision = 1;
} else if (decision == 2) {
counter.onResume1();
decision = 1;
} else {
counter.onPause1();
decision = 2;
}//end if
}
;
};
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onResume1() {
onResume();
}
public void onPause1() {
onPause();
}
public void onFinish() {
Timer.setText("00:00");
p1++;
if (p1 <= 4) {
TextView PScore = (TextView) findViewById(R.id.pscore);
PScore.setText(p1 + "");
}//end if
}//finish
public void onTick(long millisUntilFinished) {
Integer milisec = new Integer(new Double(millisUntilFinished).intValue());
Integer cd_secs = milisec/1000;
Integer minutes = (cd_secs % 3600)/60;
Integer seconds = (cd_secs % 3600) % 60;
Timer.setText(String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
///long timeLeft = millisUntilFinished/1000;
/}//on tick
}//class MyCount
protected void onResume() {
super.onResume();
//handler.removeCallbacks(updateTimeTask);
//handler.postDelayed(updateTimeTask, 1000);
}//onResume
@Override
protected void onPause() {
super.onPause();
//do stuff
}//onPause
這將surelyhelp你 http://stackoverflow.com/questions/3510433/countdown-timer-required-on-android –
的http://開發商。 android.com/reference/android/os/CountDownTimer.html –
我需要使用crono來暫停和恢復嗎? – Mineko