gpt4 book ai didi

java - 在另一个 CountDownTimer 运行时暂停一个 CountDownTimer - Android

转载 作者:行者123 更新时间:2023-12-01 09:20:41 29 4
gpt4 key购买 nike

我的程序中有两个倒计时器,一个较长的(120 秒)和一个较短的(3.5 秒)。我希望 120 秒计时器在 3.5 秒计时器运行时暂停,而较长的计时器在 3.5 秒计时器不运行时继续运行。因此,程序从剩余 120 秒开始,同时 3.5 秒的程序运行,然后当 3.5 秒的程序运行时,120 秒的程序将启动,并且仅在 3.5 秒的程序再次运行时暂停(一旦用户按 Enter 键)。我该怎么做这个?

final CountDownTimer loop = new CountDownTimer(3500, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score++;
}

number.setVisibility(View.VISIBLE);

if (score>=0 && score<=2){
int loadG1 = generateG1.nextInt(89999)+10000;
number.setText(""+loadG1);
}

else if (score>=3 && score<=5){
int loadG1 = generateG1.nextInt(899999)+100000;
number.setText(""+loadG1);
}

else if (score>=6 && score<=9){
int loadG1 = generateG1.nextInt(8999999)+1000000;
number.setText(""+loadG1);
}

else if (score>=10 && score<=14){
int loadG1 = generateG1.nextInt(89999999)+10000000;
number.setText(""+loadG1);
}

else if (score>=15 && score<=20){
int loadG1 = generateG1.nextInt(899999999)+100000000;
number.setText(""+loadG1);
}

else if (score>=21) {
int loadG1 = generateG1.nextInt((int) 8999999999L)+1000000000;
number.setText(""+loadG1);
}

input.getText().clear();

start();

return true;
default:
}
}
return false;
}
});
}
}.start();

new CountDownTimer(120000, 1000) {
@Override
public void onTick (long millisUntilFinished) {
}
@Override
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.GONE);
loop.cancel();
number.setVisibility(View.GONE);
}
}.start();

我之前曾问过这个问题,但遗憾的是没有得到有效答案。如果有人能够回答这个问题,将不胜感激。请随意插入任何有助于解释您的答案的代码。非常感谢。

最佳答案

好吧,我会尝试举一个例子,但不能保证这正是您所需要的:

创建一个全局变量和 CountDownTimer 对象:

Long remainingTime = 120000L;
ThreePointFiveSecondsTimer mThreePointFiveSecondsTimer;
HundredTwentySecondsTimer mHundredTwentySecondsTimer;

创建 120 秒计时器:

public class HundredTwentySecondsTimer extends CountDownTimer {

public HundredTwentySecondsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {


}

@Override
public void onFinish() {

}
}

创建 3.5 秒计时器:

  public class ThreePointFiveSecondsTimer extends CountDownTimer {

public ThreePointFiveSecondsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {

remainingTime = millisUntilFinished;//set the remaining time
}

@Override
public void onFinish() {

//start the 120 second countdowntimer again
mHundredTwentySecondsTimer = new MyCountDownTimer(remainingTime, 1000);
mHundredTwentySecondsTimer.start();
}
}

启动 120 秒计时器:

mHundredTwentySecondsTimer  = new MyCountDownTimer(remainingTime, 1000);
mHundredTwentySecondsTimer.start();

然后,您可以随时决定启动 3.5 计时器:

mThreePointFiveSecondsTimer   = new ThreePointFiveSecondsTimer (3500, 1000);
mThreePointFiveSecondsTimer.start();
mHundredTwentySecondsTimer.cancel();
mHundredTwentySecondsTimer = null;

这只是背后的想法,但您必须根据您的需求进行调整。抱歉,但无法为您提供所需的所有内容,这超出了框架范围。

关于java - 在另一个 CountDownTimer 运行时暂停一个 CountDownTimer - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182507/

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