繁体   English   中英

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

[英]How to implement CountDownTimer reducing time functionality in Android

我在我的活动中实现了一个 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 秒。 任何帮助,将不胜感激

如果用户输入正确,则添加一个值为 0 的全局变量,否则为 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()
    
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM