我正在Android平台上创建一个倒计时器。
当用户按下“停止”按钮时,我不知道如何将剩余时间存储在计时器上(计时器上的5分钟)。按钮(暂停)。因此,如果用户稍后按下“开始”按钮,再次按下按钮,它将从停止的位置继续倒计时。这是我到目前为止所做的。
public class Timer_App extends Activity {
Button btn_Start, btn_Stop, btn_Reset;
TextView timer_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_app);
//NEVER SLEEP!
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Buttons
btn_Start = (Button) findViewById(R.id.Start);
btn_Stop = (Button) findViewById(R.id.Stop);
btn_Reset = (Button) findViewById(R.id.Reset);
//FONT
Typeface typeface = Typeface.createFromAsset(getAssets(), "Digital_Font.TTF");
timer_text = (TextView) findViewById(R.id.timer_view);
timer_text.setTypeface(typeface);
//Timer
timer_text.setText("05:00);
final CounterClass timer = new CounterClass(300000, 1000);
//Start btn
btn_Start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
//Stop btn
btn_Stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
//Reset btn
btn_Reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
//TIMER CLASS
public class CounterClass extends CountDownTimer{
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
timer_text.setText(hms);
}
@Override
public void onFinish() {
}
}
}