【问题标题】:Get remaining time on CountdownTimer and use the remaining time as a score android在 CountdownTimer 上获取剩余时间并将剩余时间用作分数 android
【发布时间】:2015-09-18 19:49:24
【问题描述】:

所以我在这里有测验应用程序并有计时器。所以我想要发生的事情,例如我已经将计时器设置为 15 秒,如果用户在 5 秒内回答问题,我希望 10 秒的 ramaining 变为 10 分,它将添加到之前的分数加上你的分​​数回答问题。所以现在我有这个......

        if(savedInstanceState!=null){
        //saved instance state data
        int exScore = savedInstanceState.getInt("score");
        scoreText.setText("Score: "+exScore);
    }

    Timer = new CountDownTimer(15000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tv_time.setText("" + millisUntilFinished / 1000);
            int progress = (int) (millisUntilFinished / 150);
            progressBar.setProgress(progress);

        }

        @Override
        public void onFinish() {
            progressBar.setProgress(0);
            timeUp(context);

        }
    }.start();

这里是 onclick 的。如果用户回答正确,它会自动加 10 分

public void onClick(View view) {
        Button clicked = (Button) view;
        int exScore = getScore();

    if (clicked.getText().toString().equals(this.active_question.getAnswer()) ) {
        if (this.questions.size() > 0) {
                    setQuestion(questions.poll());
                    scoreText.setText("Score: " + (exScore + 10))


    } else  {
        CustomGameOver cdd = new CustomGameOver(PlayQuizActivity.this);
        cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        cdd.show();
        setHighScore();


        Timer.cancel();
        }

}

我不知道如何在 CountdownTimer 上获取剩余时间并在答案正确时将其添加为分数。谁能帮帮我。

【问题讨论】:

    标签: android countdowntimer


    【解决方案1】:

    只需使用 CountDownTimer 的 onTick 中的 millisUntilFinished

    奖金将是millisUntilFinished/1000

    P.S 我认为你最好使用低于 1000 的间隔,这样 ProgressBar 会看起来更平滑。

    【讨论】:

      【解决方案2】:

      您只需在 MainActivity 中声明一个长变量 timeleft

      long timeleft; 
      

      然后,当您创建一个新的 Timer 时,设置“onTick”覆盖以更新每个“onTick”的 timeleft 变量(在以下示例中为 1000 毫秒)

          timer = new CountDownTimer(time, 1000) {
             @Override
              public void onTick(long millisecondsUntilFinished) {
                  timeleft = millisecondsUntilFinished;
              }
            }
      

      您的应用可以在每次需要检查剩余时间时访问变量timeleft

         score = score + timeleft / 1000; // divide by 1000 to get seconds
      

      请记住,如果您需要更新计时器,则必须取消它并创建一个新的计时器,并保留更新后的时间(以及相同的覆盖);

             timeleft = timeleft + bonustime;  // (if you want to add bonus time, remember has to be in milliseconds)
             if( timer != null){ timer.cancel();} // better check first if the timer exists
             timer = new CountDownTimer(timeleft, 1000) {
                  @Override
                  public void onTick(long millisecondsUntilFinished) {
                      timeleft = millisecondsUntilFinished;
                  }
      

      【讨论】:

        猜你喜欢
        • 2018-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-02
        相关资源
        最近更新 更多