gpt4 book ai didi

java - 在android中重置递减计数器

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:12 25 4
gpt4 key购买 nike

我试图在循环中运行递减计数器,5 秒后计数器再次从 5 开始计数到 ​​0。我已经尝试过这段代码

countDownTimer.cancel();
countDownTimer.start();

但这不起作用。 textview 中的值不会重置。下面发布的是完整的代码。问候!

private void timee() {

for (int j = 5; j > 0; j--) {
countDownTimer = new MalibuCountDownTimer(5000, 1000);
if (j == 4)
countDownTimer.start();
else {
countDownTimer.cancel();
countDownTimer.start();
}
}
}

// CountDownTimer class
public class MalibuCountDownTimer extends CountDownTimer {

public MalibuCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}

@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
// takeImage();
}

@Override
public void onTick(long millisUntilFinished) {
tt.setText("Time remain:" + millisUntilFinished / 1000);
}
}

最佳答案

用以下代码替换 timee() 方法和 CountDownTimer 类

private void timee(){

MalibuCountDownTimer countDownTimer=new MalibuCountDownTimer(5000, 1000);
countDownTimer.start();

}
public class MalibuCountDownTimer extends CountDownTimer {

public MalibuCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}

@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
j++;
if(j<=5)
start();
}

@Override
public void onTick(long millisUntilFinished) {
m_tv_emp.setText("Time remain:" + millisUntilFinished / 1000);
}
}

注意:将j作为类变量

关于java - 在android中重置递减计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com