gpt4 book ai didi

android - 等待计时器不会在按钮单击时重新启动

转载 作者:行者123 更新时间:2023-11-30 02:25:26 27 4
gpt4 key购买 nike

在我的应用程序中,我想要一个按钮来启动倒数计时器,例如:

@Override
public void onClick(View v) {
switch (v.getId()) {

case R.id.Next:

// Define CountDown Timer Attributes//
waitTimer1 = new CountDownTimer(45191, 1000) {


@Override
public void onTick(long millisUntilFinished) {
long timeLeft = millisUntilFinished / 1000;
Timer.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

}

@Override
public void onFinish() {

}

}.start();


// Repeat Words//
if (word.getText().toString().equals("Big Foot")) {

stringListCounter = 0;

}
// Change To Next Word//
stringListCounter++;
word.setText(stringIdList[stringListCounter]);

break;
}

}
}

一旦计时器第一次启动,我希望能够再次按下该按钮,而计时器不会重新开始。

例如(假的例子只是为了解释)像

 if(timer has started) {

doNothingToTimer;
}

最佳答案

我认为问题在于每次单击按钮时都会重新初始化时间。

@Override
public void onClick(View v) {
switch (v.getId()) {

case R.id.Next:
if (waitTimer1 == null){
// Define CountDown Timer Attributes//
waitTimer1 = new CountDownTimer(45191, 1000) {


@Override
public void onTick(long millisUntilFinished) {
long timeLeft = millisUntilFinished / 1000;
Timer.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

}

@Override
public void onFinish() {

}

}.start();
}//end-if
else{
//
//do other button stuff
}


// Repeat Words//
if (word.getText().toString().equals("Big Foot")) {

stringListCounter = 0;

}
// Change To Next Word//
stringListCounter++;
word.setText(stringIdList[stringListCounter]);

break;
}

}

关于android - 等待计时器不会在按钮单击时重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27952835/

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