0

我的倒数计时器没有使用常量值,因为每个下一个级别的时间都不同。所以我将 timeCount 变量设置为 150000 用于第一级:

    long timeCount = 150000;

public String formatTime(long millis) {  
                    String output = "00:00";  
                    long seconds = millis / 1000;  
                    long minutes = seconds / 60;  

                    seconds = seconds % 60;  
                    minutes = minutes % 60;  

                    String sec = String.valueOf(seconds);  
                    String min = String.valueOf(minutes);  

                    if (seconds < 10)  
                        sec = "0" + seconds;  
                    if (minutes < 10)  
                        min= "0" + minutes;  

                    output = min + " : " + sec;  
                    return output;
                }

还有我的计时器:

public class MyCount extends CountDownTimer {
         public MyCount(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);
         }

         public void onFinish() {


         }
         public void onTick(long millisUntilFinished) {
             vreme.setText("" + millisUntilFinished / 1000);
         }
         }
    // New timer for 40 minutes, starts after initialization
   MyCount brojacVremena = new MyCount(timeCount, 1000) 
   {
       // Updates the text on your "scoreboard" every second
       public void onTick(long millisUntilFinished) 
       {
           vreme.setText("" + formatTime(millisUntilFinished));
       }

       public void onFinish() 
       {
        finish();
       }
   };

所以,当游戏结束时,我会显示一个弹出窗口,按下 OK 后会进入下一个级别。我调用弹出窗口并设置新值,用于时间和其他一些东西:

Intent i = new Intent(Kviz.this, Popup_nivoi.class);
            if(level==2){
                numberOfQuestions = 13;
                timeCount = 160000;
                greska = 7;
                level++;
}
brojacVremena.cancel();
            startActivityForResult(i, MY_REQUEST2);

但我的倒数计时器总是从旧时间开始,150000 毫秒。如何重置它,导致我更改 timeCount 值不起作用。我不知道为什么。该变量已更改,我肯定知道,但倒数计时器不使用它。

4

1 回答 1

2

您不能在已经创建CountDownTimer的不可变对象中重置时间,并且您的timeCount变量在创建此对象时仅使用一次。因此,您需要做的只是创建另一个具有新时间的计时器。

UPD:这是一些代码

public class MyCount extends CountDownTimer {
     public MyCount(long millisInFuture, long countDownInterval) {
         super(millisInFuture, countDownInterval);
     }

     public void onFinish() {
         finish();
     }

     public void onTick(long millisUntilFinished) {
         vreme.setText("" + formatTime(millisUntilFinished));
     }
}
MyCount brojacVremena = new MyCount(timeCount, 1000);

然后,当您需要新计时器时,您只需编写

brojacVremena = new MyCount(timeCount, 1000);

带有新timeCount变量。

于 2013-07-23T16:20:35.283 回答