等待计时器不会在按钮单击时重新启动

wait timer not restart on button click

在我的应用程序中,我想要一个按钮来启动倒数计时器,例如:

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.Next:

            // Define CountDown Timer Attributes//
            waitTimer1 = new CountDownTimer(45191, 1000) {


                @Override
                public void onTick(long millisUntilFinished) {
                    long timeLeft = millisUntilFinished / 1000;
                    Timer.setText("" + String.format("%d min, %d sec",
                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

                }

                @Override
                public void onFinish() {

                }

            }.start();


            // Repeat Words//
            if (word.getText().toString().equals("Big Foot")) {

                stringListCounter = 0;

            }
            // Change To Next Word//
            stringListCounter++;
            word.setText(stringIdList[stringListCounter]);

           break;
        }

    }
}

一旦计时器第一次启动,我希望能够再次按下该按钮,而计时器不会重新开始。

例如(假的例子只是为了解释)像

 if(timer has started) {

     doNothingToTimer;
}

我认为问题在于每次单击按钮时都会重新初始化时间。

@Override
public void onClick(View v) {
switch (v.getId()) {

    case R.id.Next:
        if (waitTimer1 == null){
        // Define CountDown Timer Attributes//
        waitTimer1 = new CountDownTimer(45191, 1000) {


            @Override
            public void onTick(long millisUntilFinished) {
                long timeLeft = millisUntilFinished / 1000;
                Timer.setText("" + String.format("%d min, %d sec",
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

            }

            @Override
            public void onFinish() {

            }

        }.start();
        }//end-if
        else{
            //
            //do other button stuff
        }


        // Repeat Words//
        if (word.getText().toString().equals("Big Foot")) {

            stringListCounter = 0;

        }
        // Change To Next Word//
        stringListCounter++;
        word.setText(stringIdList[stringListCounter]);

       break;
    }

}

}