Java倒计时器 - 一个接一个

时间:2014-06-25 12:50:20

标签: java android intervals countdowntimer

我是supernoob程序员。我试图建立一个adroid间隔计时器,用户输入轮次,工作时间和休息时间。我找不到一种在工作计时器后启动中断计时器的方法,它们会同时开始运行。我应该怎么做呢?

谢谢!

public class MainActivity extends Activity {

    EditText rundy, praca, przerwa;
    Button start;
    TextView timer;
    boolean timerHasStarted = false;

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


        rundy = (EditText) findViewById(R.id.rundy); // rounds
        praca = (EditText) findViewById(R.id.praca); // work time
        przerwa = (EditText) findViewById(R.id.przerwa); // break time
        start = (Button) findViewById(R.id.start);
        timer = (TextView) findViewById(R.id.timer);
        start.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {


                int rundyL = Integer.parseInt(rundy.getText().toString())*1000;
                long pracaL = Long.parseLong(praca.getText().toString()) * 1000;
                long przerwaL = Long.parseLong(przerwa.getText().toString())*1000;
                MyCountDownTimer countDownTimerPraca = new MyCountDownTimer(pracaL, 1000);
                MyCountDownTimer countDownTimerPrzerwa = new MyCountDownTimer(przerwaL,1000);

                for(int i = 0; i<rundyL; i++){

                countDownTimerPraca.start();                
                countDownTimerPrzerwa.start();
                }
            }

            class MyCountDownTimer extends CountDownTimer {
                public MyCountDownTimer(long pracaL, long interval) {
                    super(pracaL, interval);
                }

                @Override
                public void onTick(long millisUntilFinished) {
                    timerHasStarted = true;
                    long minutes = (millisUntilFinished / 1000)/60;
                    long seconds = (millisUntilFinished/1000)%60;
                    timer.setText( String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
                }

                @Override
                public void onFinish() {
                    timerHasStarted = false;
                    timer.setText("Times up");
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {

    EditText rundy, praca, przerwa;
    Button start;
    TextView timer;
    boolean timerHasStarted = false;

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


        rundy = (EditText) findViewById(R.id.rundy); // rounds
        praca = (EditText) findViewById(R.id.praca); // work time
        przerwa = (EditText) findViewById(R.id.przerwa); // break time
        start = (Button) findViewById(R.id.start);
        timer = (TextView) findViewById(R.id.timer);
        start.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {


                int rundyL = Integer.parseInt(rundy.getText().toString())*1000;
                long pracaL = Long.parseLong(praca.getText().toString()) * 1000;
                long przerwaL = Long.parseLong(przerwa.getText().toString())*1000;
                MyCountDownTimer countDownTimerPrzerwa = new MyCountDownTimer(przerwaL,1000);
                MyCountDownTimer countDownTimerPraca = new MyCountDownTimer(countDownTimerPrzerwa,pracaL, 1000);
                for(int i = 0; i<rundyL; i++){
                countDownTimerPraca.start();
                }
            }

            class MyCountDownTimer extends CountDownTimer {
                private MyCountDownTimer mytimer;
                public MyCountDownTimer(long pracaL, long interval) {
                    super(pracaL, interval);
                }
                 public MyCountDownTimer(MyCountDownTimer timer, long pracaL, long interval) {
                    this(pracaL, interval);
                    this.timer = timer;
                }

                @Override
                public void onTick(long millisUntilFinished) {
                    timerHasStarted = true;
                    long minutes = (millisUntilFinished / 1000)/60;
                    long seconds = (millisUntilFinished/1000)%60;
                    timer.setText( String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
                }

                @Override
                public void onFinish() {
                    timerHasStarted = false;
                    timer.setText("Times up");
                    if(mytimer!=null)
                        mytimer.start();
                }
            }
        });
    }
}