gpt4 book ai didi

Java/Android 倒数计时器

转载 作者:行者123 更新时间:2023-11-30 01:45:24 24 4
gpt4 key购买 nike

我正在尝试使用 Java 在 Android 上制作倒数计时器。有一个计时器,一个 25 分钟的学习计时器和一个 5 分钟的休息计时器。我有两个计时器都在工作。但是,我不知道如何取消/清除计时器。如果我单击我启动学习计时器和中断计时器,它们将同时运行,我不希望这样。我希望在打开另一个计时器时取消原来的计时器。这是代码。

btnStart.setOnClickListener(新的 OnClickListener() {

        public void onClick(View v){

btnStart.setEnabled(false);
btnBreak.setEnabled(true);

breakBoolean = false;




CountDownTimer timer;
long amountOfStudyTime = 1500000; //30 seconds (may cause problems)
long countDownInterval = 1000; //1 second

//Initialise the countdown timer

timer = new CountDownTimer(amountOfStudyTime, countDownInterval){

public void onTick(long millisUntilFinished){

if( studyBoolean = false ) {

tViewTime.setText("CountDownTimer Canceled/stopped.");
cancel();
breakBoolean = true;

}else{
//display remaining seconds to user

tViewTime.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

}
}

public void onFinish(){
//When countdown is finished do...

breakBoolean = true;

int currentScore = Integer.parseInt(editScore.getText().toString());

int finalScore = currentScore + 5;

editScore.setText(Integer.toString(finalScore));


tViewTime.setText("Done");

}

}.start();
}
});



//Set a click listener for break button
btnBreak.setOnClickListener(new OnClickListener() {

public void onClick(View v){



btnStart.setEnabled(true);
btnBreak.setEnabled(false);


studyBoolean = false;



CountDownTimer timer2;
long amountOfBreakTime = 300000; //30 seconds (may cause problems)
long countDownInterval = 1000; //1 second

//Initialise the countdown timer

timer2 = new CountDownTimer(amountOfBreakTime, countDownInterval){

public void onTick(long millisUntilFinished){

if( breakBoolean = false ) {


cancel();
studyBoolean = true;

}else{
//display remaining seconds to user

tViewTime.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

}
}

public void onFinish(){
//When countdown is finished do...


tViewTime.setText("Done");
studyBoolean = true;
}

}.start();
}
});

最佳答案

在您的 Activity 中创建两个 CountDownTimer 对象并根据您选择的按钮取消它们:

    public class MainActivity extends Activity {

boolean breakIsRunning = false;
boolean startIsRunning = false;

Button btnStart,btnBreak;

CountDownTimer startTimer = new CountDownTimer(amountOfStudyTime, countDownInterval)
{

@Override
public void onFinish() {
//do something
startIsRunning = false;
}

@Override
public void onTick(long arg0) {
//do something
startIsRunning = true;
}

};



CountDownTimer breakTimer = new CountDownTimer(amountOfBreakTime, countDownInterval)
{

@Override
public void onFinish() {
//do something
breakIsRunning = false;
}

@Override
public void onTick(long arg0) {
//do something
breakIsRunning = true;
}

};


//->OnCreate() - >Buttons code

btnStart.setOnClickListener(new OnClickListener().. { // your listener code here
if(breakIsRunning)
breakTimer.cancel();
startTimer.start();
}

btnBreak.setOnClickListener(new OnClickListener().. { //
if(startIsRunning)
startTimer.cancel();
breakTimer.start();
}

}

关于Java/Android 倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792211/

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