倒数计时器,具有可由用户设置的递增/递减选项

时间:2011-12-15 11:25:55

标签: java android timer count countdown

我是Java语言的新手,但之前使用的是C语言。我尝试了很多方法来解决以下问题,但不能这样我需要帮助。 我正在尝试执行以下操作:

•默认设置5分钟计时器(计数器),因此在按下Start_button时启动。

•如果未按下Start_button并且用户按下Up_button / Down_button,则在屏幕上显示计时器选项; 5,10和15分钟,如果用户按下Starts_button,它开始倒计时所选的计时器。

•计时器正在运行;

  •If Up_button /Down_button is pressed once then reset timer and show previous time setting i.e. 5, 10 or 15minutes.
  •If Up_button /Down_button is pressed again then display timer options on screen and if the user presses Starts_button it starts the timer chosen.  

此刻;一旦按下开始计时器正在工作,它会倒计时5分钟。但我不知道显示计时器选项的最佳方式,也启动所选的计时器。

非常感谢您的帮助,非常感谢您。

如上所述;我正在学习Java编程,所以请放轻松;-)并告诉我你认为最适合这个问题的代码

这是我到目前为止所做的:

public class Test extends Activity {

// Display Counter Variables
public static Button Up, Down, Green;

TextView timeDisplay;
MyCount counter, counter1, counter2;
int state = 0;
int length = 300000; //5minutes
int length1 = 600000; //10minutes
int length2 = 900000; //15minutes
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
boolean up_pressed = false;
boolean down_pressed = false;
private boolean timerStarted=false;

Button start;

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

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

        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);

        if (seconds < 10)
          secondsD = "0" + seconds;
        if (minutes < 10)
          minutesD = "0" + minutes;

        output = minutesD + " : " + secondsD;
        return output;
      }


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);


    timeDisplay = (TextView) findViewById(R.id.timer);
    counter = new MyCount (length, 1000);
    counter1 = new MyCount (length1, 1000);
    counter2 = new MyCount (length2, 1000);

    start = (Button) findViewById(R.id.Button);
    Up = (Button)findViewById(R.id.Yellow);
    Down = (Button)findViewById(R.id.Blue);



start.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        switch (state) {
        case 0:
          startTime = System.currentTimeMillis();
            counter.start();
            timerStarted=true;
            start.setText(R.string.pause);
              state=1;

          break;

       case 1:
          // pause
          currentTime = System.currentTimeMillis();
          timeElapsed = currentTime - startTime;
          if (prevTimeRemaining == 0)
            timeRemaining = length - timeElapsed;
          else
            timeRemaining = prevTimeRemaining - timeElapsed;
          counter.cancel();
          timeDisplay.setText("" + formatTime(timeRemaining));
          start.setText(R.string.resume);
          prevTimeRemaining = timeRemaining;

          // resume
          counter = new MyCount(timeRemaining, 1000);
          state = 0;
          break;

        case 2:
          prevTimeRemaining = 0;
          timerStarted=false;
          counter = new MyCount(length, 1000);
          start.setText(R.string.start);
          timeDisplay.setText(R.string.timer);
          state = 0;
        }
    }
});


    Up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            down_pressed=true;

            if(up_pressed=true && timerStarted==true 
                || timerStarted==false){
                //Display timer (increment i.e. show 5min --> 10min ..> 15min)

                //start timer chosen by user
            }

        }
    });


    Down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            down_pressed=true;
            if(down_pressed=true && timerStarted==true 
                || timerStarted==false){
                //Display timer (decrement i.e. show 15min --> 10min ..> 5min)

                //start timer chosen by user
            }
        }
    });
}


  public class MyCount extends CountDownTimer {

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

      public void onFinish() {
        timeDisplay.setText("done!");
        state = 2;
       start.setText(R.string.restart);
      }

      public void onTick (long millisUntilFinished) {
        timeDisplay.setText ("Left: "  + formatTime(millisUntilFinished));

      }

    }

}

1 个答案:

答案 0 :(得分:0)

我认为你不需要新的类。

我只想创建一个全局变量,例如

private CountDownTimer myCount;

我建议将其放入类似

的方法中
private void setTimer(long countdownMs, long tickMs) {
  CountDownTimer myCount = new CountDownTimer(countdownMs,tickMs) {
  /// TODO
  }.start();
}

如果您需要重启计时器,可以拨打

myCount.cancel()
setTimer(...);

如果您需要更多详细信息,请注释:)