如何在countdowntimer中实现暂停和恢复方法 [英] how to implement pause and resume method in countdowntimer

查看:2698
本文介绍了如何在countdowntimer中实现暂停和恢复方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用倒数计时器概念。在这里,当我单击开始按钮时,倒计时将开始。以及也停止其正常工作,但我需要在我的倒数计时器中实现暂停和恢复方法。当我单击暂停按钮时,倒计时将进入暂停状态。当我单击开始按钮时,倒计时将从暂停值开始。例如:我的倒数是0到30,假设如果我单击暂停按钮,则该值将是暂停,例如:25或15。当我单击开始按钮时,倒数将从25开始。我希望大家都理解我尝试过的问题,但是我没有任何解决办法,请您帮忙

Hi i am working countdowntimer concept. Here when i click start button the countdown will start. as well as stop also its working fine but i need to implement pause and resume method in my countdown timer. the countdown will go to pause state when i click pause button. the countdown will start from pause value when i click start button. for ex: my countdown is 0 to 30, suppose if i click pause button the value will be pause ex: 25 or 15 something else. the countdown will start from 25 when i click start button. i hope you all understand my problem i tried but i am not getting any solution could you please help me

  public class MainActivity extends ActionBarActivity implements OnClickListener {

     private CountDownTimer countDownTimer;
     private boolean timerHasStarted = false;
     private Button startB;
     public TextView text;
     private final long startTime = 30 * 1000;
     private final long interval = 1 * 1000;


     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener((OnClickListener) this);
    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime / 1000));
   }

    public void onClick(View v) {
     if (!timerHasStarted) {
      countDownTimer.start();
      timerHasStarted = true;
      startB.setText("STOP");
      } else {
        countDownTimer.cancel();
        timerHasStarted = false;
        startB.setText("RESTART");
    }
    } 
 public class MyCountDownTimer extends CountDownTimer {
   public MyCountDownTimer(long startTime, long interval) {
                   super(startTime, interval);
  }

   @Override
    public void onFinish() {
     //text.setText("Time's up!");
      countDownTimer.start();
   }

     @Override
      public void onTick(long millisUntilFinished) {
      text.setText("" + millisUntilFinished / 1000);
         }
       }
    }


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/rl" >

    <TextView
    android:id="@+id/timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingRight="10dip"
    android:textSize="50dp" />

    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Start" />

</RelativeLayout>


推荐答案

我看到的唯一方法是取消计时器调用暂停并在需要恢复时创建一个新的暂停,例如:

The only way I see is to cancel timer when you call pause and create a new one when you want to resume it, something like this :

public class MainActivity extends ActionBarActivity implements OnClickListener {

 private MyCountDownTimer countDownTimer;
 private boolean timerHasStarted = false;
 private Button startB;
 public TextView text;
 private final long startTime = 30 * 1000;
 private final long interval = 1 * 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener(this);

    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval,text );
    // Set starting value
    text.setText(text.getText() + String.valueOf(startTime / 1000));
}

 public void onClick(View v) {
     if (!timerHasStarted) {
      // Start or resume counter
      countDownTimer = countDownTimer.resume();
      timerHasStarted = true;
      startB.setText("PAUSE");
      } else {
       // Pause counter
        countDownTimer.pause();
        timerHasStarted = false;
        startB.setText("RESTART");
      }
}

public class MyCountDownTimer extends CountDownTimer
{

    private long mRemainingTime = 0;
    private long mInterval = 0;
    private TextView mtvxToUpdate = null

    //  Use a textview reference to update text on each tick()
    public MyCountDownTimer(long startTime, long interval,TextView txv ) {
        super(startTime, interval);
        this.mInterval = interval;
        this.mRemainingTime = startTime;
        this.mtvxToUpdate = txv;
    }

    @Override
    public void onFinish() {
        mtvxToUpdate.setText("Time's up!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
            mRemainingTime = millisUntilFinished;           
            mtvxToUpdate.setText("" + millisUntilFinished / 1000);
    }


    public void pause(){
        // Stop current timer 
        this.cancel();
    }

    public MyCountDownTimer resume(){
        // Create a counter with last saved data (just before pause)
        MyCountDownTimer newTimer = new MyCountDownTimer(mRemainingTime,mInterval,mtvxToUpdate);
        // Start this new timer that start where old one stop
        newTimer.start();
        // Return this new timer
        return newTimer;
    }
}
}

这篇关于如何在countdowntimer中实现暂停和恢复方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆