如何在Android中实现CountDownTimer减少时间功能



我在活动中实现了一个CountDownTimer,它运行并显示15分钟的计时器。我想添加一个功能,如果用户输入的输入不正确,时钟上剩余的时间将减少20秒。CountDownTimer的实现如下所示:

//            counter = TimerClass.getInstance();
//            System.out.println("Timer: " + counter.getFormatedTime());
//            ((TextView) findViewById(R.id.time)).setText(counter.getFormatedTime());
new CountDownTimer(Activity_Game.time, 1000){
@Override
public void onTick(long millisUntilFinished) {
Activity_Game.time = (int) millisUntilFinished;
Log.d(TAG, "onTick: "+Activity_Game.time);
millisUntilFinished = millisUntilFinished/1000;
((TextView) findViewById(R.id.time)).setText(String.format("Time: %02d:%02d",
(millisUntilFinished % 3600) / 60, (millisUntilFinished % 60)) + "");
@Override
public void onFinish() {
// Don't need to finish twice as activity game has timer ticking on the same time so 
it will finish this activity and pop up game over/vicotry
}
}.start();
} catch (Exception exception) {
exception.printStackTrace();
}

我在以下代码中检查输入是否与我的参数匹配:

public void onClickSubmit(View view){
//Get user entered text
EditText ansText = findViewById(R.id.answerText);
String userInput = ansText.getText().toString();
//choice 4 ans contains comma
//
double RightansInDouble;
double UseansInDouble;
try {
if (currentAnswer.contains(",")) {
//            RightansInDouble = Double.parseDouble(currentAnswer);
UseansInDouble = Double.parseDouble(userInput);
String[] ans = currentAnswer.split(",");
double ans1 = Double.parseDouble(ans[0]);
double ans2 = Double.parseDouble(ans[1]);
//If the answer is right
if (Math.abs(ans1 - UseansInDouble) <= 0.5 || Math.abs(ans2 - UseansInDouble) <= 0.5) {
//Do something
//Increment in points
TextView score = findViewById(R.id.Currentscore);
currentScore = Integer.parseInt(score.getText().toString());
currentScore+= 2;
activity_game.score+= 2;
score.setText(activity_game.score + "");
//new question to be displayed
nextQuestion();
} else {
//if the use entered answer is incorrect
// go down a stage
//                    System.out.println("Wrong answer");
if(Activity_Game.score > 0)
Activity_Game.score-=2;

Toast t = Toast.makeText(Activity_Room.this, "Wrong Answer! Try again!", Toast.LENGTH_SHORT);
t.show();
}
} else {
RightansInDouble = Double.parseDouble(currentAnswer);
UseansInDouble = Double.parseDouble(userInput);
//If the answer is right
if (Math.abs(RightansInDouble - UseansInDouble) <= 0.5) {
//Do something
//Increment in points
TextView score = findViewById(R.id.Currentscore);
currentScore+= 2;
activity_game.score+= 2;
score.setText(activity_game.score + "");
//new question to be displayed
nextQuestion();
} else {
//if the use entered answer is incorrect
//                    System.out.println("Wrong answer");
if(Activity_Game.score > 0)
Activity_Game.score-=2;
Toast t = Toast.makeText(Activity_Room.this, "Wrong Answer! Try again!", Toast.LENGTH_SHORT);
t.show();
}
}
}
catch (Exception e){
Toast t = Toast.makeText( Activity_Room.this, "Invalid Input! Use only digits and points if necessary!", Toast.LENGTH_SHORT);
t.show();
//            System.out.println("Invalid Input");
}
//for testing purpose
//        System.out.println("Clicked submit button");
//        nextQuestion();
}

我不知道如何继续实施一个系统,一旦检测到错误答案,计时器时钟就会减少20秒。如有任何帮助,将不胜感激

如果用户输入正确,则添加一个值为零的全局变量,否则其值为20。在计时器的onTick((方法中使用此变量,将其从剩余时间中减去。

您可以使用此代码为我工作

private fun otpCountdown() {
val countDownTimer = object : CountDownTimer(60000, 1000) {
override fun onTick(p0: Long) {
val millis: Long = p0
val hms = String.format(
"%02d:%02d",
(TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))),
(TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millis)
))
)
textView?.text = hms;
}

override fun onFinish() {
textView?.text = "Resend";
}
}
countDownTimer.start()

}

最新更新