如何暂停countDown计时器,当我点击开始时,它从停在它上面的最后一个点开始,并希望pauseButton暂停这个倒计时器同时点击暂停计数计时器。那么这个代码是什么?
public class one extends Activity {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
TextView mButtonLabel;
private long mStartTime = 0L;
private TextView mTimeLabel, mTimerLabel;
private Handler mHandler = new Handler();
static final int UPDATE_INTERVAL = 1000;
String timerStop1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mTimeLabel = (TextView) findViewById(R.id.countDownTv);
mButtonLabel = (TextView) findViewById(R.id.countDown);
// mTimerLabel = (TextView) findViewById(R.id.textTimer);
mButtonLabel = (TextView) findViewById(R.id.countDown);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
CountDownTimer timer1 = new CountDownTimer(1800000, 1000) {
@Override
public void onFinish() {
mTimeLabel.setText("Done");
}
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
mTimeLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
}
}.start();
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
// here i want to put the code that will make this click pause the countdown timer also
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":" + String.format("%02d", secs)
+ ":" + String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
mTimerLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
timerStop1 = minutes + ":" + String.format("%02d", seconds);
mHandler.postDelayed(this, 200);
}
};
}
答案 0 :(得分:0)
我能想到的最大脑死亡解决方案是在onTick()方法中保存倒数计时器的当前值,然后再根据此值设置计时器。
编辑:
public void pause() {
this.timer.cancel();
}
public void resume() {
this.setupTimer(timeLeft, interval);
}
此外,您可以编写一些可在单击按钮时调用的代码。