【问题标题】:Android: Resume on Count Down TimerAndroid:恢复倒数计时器
【发布时间】:2011-12-02 04:27:42
【问题描述】:

我是 Java 和 Android 编程的初学者。我对如何恢复我的倒数计时器感到困惑 在pause. counter.ontick(milisUntilFinished); 在我的代码中不起作用之后。
请帮帮我,我有点着急。

这是我的代码:

@Override  
protected void onRestoreInstanceState(Bundle cute) {  
super.onRestoreInstanceState(cute);}  

@Override  
protected void onSaveInstanceState(Bundle outState) {  
super.onSaveInstanceState(outState);}  

Timer.setOnClickListener(TimerClickListener);  
counter = new MyCount(orig, 1000);  
private OnClickListener TimerClickListener = new OnClickListener() {  

public void onClick(View v) {  
if (p1<=4){  
if (decision==0){  
counter.start();  
decision=1;}  
else if(decision==1){  
//pause
counter.cancel();  
decision=2; }  
else{  
//resume  
counter.onTick(orig);  
decision=1;   
}  
//end if  
}   } };      
class MyCount extends CountDownTimer {  
public MyCount(long millisInFuture, long countDownInterval) {  
super(millisInFuture, countDownInterval);  
public void onFinish() {  
//do stuff  
}//end if  
}//finish  
public void onTick(long millisUntilFinished) {  
. . .  
. . .    
orig = millisUntilFinished;  
Timer.setText(String.format("%02d", minutes) + ":"  
+ String.format("%02d", seconds));  
}//class MyCount  

任何帮助将不胜感激!

【问题讨论】:

标签: android


【解决方案1】:

您最好使用Chronometer 视图。示例为here

【讨论】:

    【解决方案2】:

    您不能暂停倒数计时器。一旦你使用

    counter.cancel();  
    

    你实际上完全取消了计时器。

    您实际上可以做的是在您决定恢复时启动一个新的倒数计时器,使用新参数偏离路线。

    【讨论】:

    • 我按照你说的做了,但它重置而不是恢复 //resume countresume = new MyCount(timeleft, 1000);计数器.start();决定=1; } timeleft 应该包含计时器停止时的值。
    • 当定时器暂停时,存储值(desiredTime - timeTickedUntilPaused),然后当你想恢复定时器时...用这个存储的值重新启动一个新的定时器。
    • 对不起,我对如何存储变量感到困惑。我所做的是重置而不是恢复。感谢您的帮助!
    【解决方案3】:
     final CountDownTimer cdt=new CountDownTimer(x,y)
                {
                     public void onTick(long millisUntilFinished){
                     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                     c= millisUntilFinished;
                     if(cover==6)
                     onFinish();
    
                }
                     public void onFinish()
                     {                                   
                     Integer i=(Integer) cover*10;
                     String s=i.toString();
                     mTextField.setText("done! "+s);
                     for(int k=1;k<=12;k++)
                           b[k].setClickable(false);
    
                    }            
            }.start();
    
    
    > define c as long variable assign it to the value of millisUntilFinished and restart the counter with c value as the starting value....
    
           final CountDownTimer cdt1=new CountDownTimer(c,y)
                            {
                                 public void onTick(long millisUntilFinished){
                                 mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                                 c= millisUntilFinished;
                                 if(cover==6)
                                 onFinish();
                            }
                                 public void onFinish()
                                 {                                   
                                  Integer i=(Integer) cover*10;
                                  String s=i.toString();
                                  mTextField.setText("done! "+s);
                                 }           
                            }.start();
    
    this code will virtually give u a Countdowntimer with pause and resume control
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多