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

发布于 2024-12-21 09:10:07 字数 4811 浏览 0 评论 0原文

我是 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));

      }

    }

}

I’m new to Java language but worked with C language previously. I tried many ways to solve the following problem but couldn’t so I need help.
I’m trying to do the following:

•Set 5 minute timer (counter) as default so starts when Start_button is pressed.

•If Start_button is not pressed and the user presses Up_button /Down_button then display timer options on screen; 5, 10 and 15minutes and if the user presses Starts_button it starts to count down the timer chosen.

•While timer is running;

  •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.  

At the moment; the timer is working once Start is pressed it counts down the 5 minutes. But I do not know the best way to display the timer options and also start the chosen timer.

Your help is appreciated, thank you very much in advance.

As mentioned above; I’m in the process of learning Java programming so take it easy on me ;-) and show me the code that you think is best for this problem please

This is what I did so far:

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));

      }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2024-12-28 09:10:07

我认为你不需要为此创建一个新课程。

我会简单地创建一个全局变量,例如。

private CountDownTimer myCount;

我建议将其放入类似的方法中,

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

如果您需要重新启动计时器,

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

如果您需要更多详细信息,可以调用 Comment :)

I don't think you need a new Class for this.

I would simply create a global variable, eg.

private CountDownTimer myCount;

I would recommend putting this into a method like

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

If you need to restart the timer you can call

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

Comment if you need more details :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文