gpt4 book ai didi

android - 无法让倒数计时器在我的程序中工作或显示

转载 作者:行者123 更新时间:2023-11-30 00:23:55 25 4
gpt4 key购买 nike

我是一名新程序员,这是我的第一个项目,但我在使用三个应该一个接一个运行的计时器制作适当的循环时遇到了一些麻烦。我设法让对象在循环中保存它们应该保存的值,但出于某种原因,计时器没有像它应该的那样显示在文本字段中。

          startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("mTimer:", String.valueOf(mTimer));
Log.i("mReps:", String.valueOf(mReps));
Log.i("Flexion:", String.valueOf(flexionTimer));
Log.i("Hold:", String.valueOf(holdTimer));
Log.i("Extension:", String.valueOf(extensionTimer));

for (int iter = 0; iter < mReps; iter++) {
Log.i("Loop:", String.valueOf(iter));

final Timer workingFlexionTimer = new Timer();
workingFlexionTimer.schedule(new TimerTask() {
int counter = ((int) flexionTimer / 1000);
@Override
public void run () {
runOnUiThread(new Runnable() {
@Override
public void run() {
mPhase.setText("Flexion");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingFlexionTimer.cancel();
}
}
}, 0, 1000);


final Timer workingHoldTimer = new Timer();
workingHoldTimer.schedule(new TimerTask() {
int counter = ((int) holdTimer / 1000);
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mPhase.setText("Hold!!!");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingHoldTimer.cancel();
}
}
}, flexionTimer, 1000);

final Timer workingExtensionTimer = new Timer();
workingExtensionTimer.schedule(new TimerTask() {
int counter = ((int) extensionTimer / 1000);
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mPhase.setText("Extension");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingExtensionTimer.cancel();
}
}
}, (flexionTimer + holdTimer), 1000);
}

此时我有点不知所措,如有任何建议,我们将不胜感激。

最佳答案

使用处理器

    private void Timer() {        handler = new Handler();        Run =new Runnable() {        @Override        public void run() { //Do something after 10 sec        Toast.makeText(getActivity(), "Timer called!",Toast.LENGTH_LONG).show();        Timer(); // Do again                        }};        handler.postDelayed(Run , 10000);  // 10 sec    }

关于android - 无法让倒数计时器在我的程序中工作或显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757632/

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