我对倒计时计时器是新手,所以我对这个问题一无所知。我试了很多东西,但没有得到我想要的。这是我的定时器代码。像往常一样,它是类中的一个类。
// TIMER
public class Timer extends CountDownTimer {
public Timer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
//getNgo(true, score, tries, secLeft);
}
@Override
public void onTick(long millisUntilFinished) {
//secLeft = millisUntilFinished;
int sec = (int) (millisUntilFinished / 1000);
sec = sec % 60;
int min = sec / 60;
tvTime.setTextColor(Color.WHITE);
if (sec <= 10) {
animScale(tvTime);
tvTime.setTextColor(Color.RED);
tvTime.setText("" + min + ":" + sec);
if (sec < 10) {
tvTime.setTextColor(Color.RED);
tvTime.setText("" + min + ":0" + sec);
}
} else {
tvTime.setText("" + min + ":" + sec);
}
}
}所以,我只想知道当我按下按钮时,如何扣除3秒(即3000毫秒),文本视图显示的计时器将继续计时,但时间已经被扣除。我该把代码放在哪里。谢谢!
发布于 2013-02-25 00:55:52
当我不得不这样做的时候,任务被安排在固定的时间发生,我已经:
我怀疑这是一个比您使用Timer更标准的模式。
例如:
private final Runnable task = new Runnable() { @Override public void run() { /* ... */ } };
private final ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor();
private final long initialSeconds = 3;
public void submitTask() {
stpe.schedule(task, initialSeconds, TimeUnit.Seconds());
}
public void subtractSeconds(long sec) {
if(stpe.remove(task)) {
stpe.schedule(task, Math.Max(initialSeconds - sec, 0), TimeUnit.Seconds);
}
}你需要弄清楚:
final任务或将该任务设置为发布于 2015-12-17 19:35:51
我已经使用其中一个stackoverflow帖子实现了一个简单的倒计时
// gets current time
long timeNow = System.currentTimeMillis();
/* timer holds the values of the current second the timer should display
* requiredTime is the start value that the countdown should start from
* startTime is the time when the application starts
*/
timer = requiredTime - (timeNow - startTime) / 1000;
if (timer >= 0)
timer.setText(String.valueOf(timer));要减去计时器,减去requiredTime就行了。因为您已经更改了参考值。
// Override onClickListener and add the line
// to deduct 3 seconds
requiredTime -= 3; 发布于 2013-02-25 00:53:29
你不能。你必须写你自己的CountDownTimer。复制原始代码并添加方法
public synchronized void addTime(long millis) {
mStopTimeInFuture += millis;
}然后将onClickListener设置为该按钮
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.addTime(-2000);
}
});Here是完整的示例代码
https://stackoverflow.com/questions/15053574
复制相似问题