按下按钮后如何使倒数计时器更快更快地运行

时间:2015-07-06 10:21:19

标签: android timer countdowntimer

我正在制作一个用户定时回答问题的简单游戏。

在每个正确的问题之后,用户的时间减少了。

现在,用户以 10s 的时间开始。

如何让我的countDownTimer在每个正确答案后减去时间,让 0.8

我必须提到我的计时器也会调整进度条。

这是我现在的代码:

...
private ProgressBar mProgressBar;
private CountDownTimer mCountDownTimer;
private long timer_length = 10*1000;
private long timer_interval = 500;
...
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 mProgressBar = (ProgressBar)findViewById(R.id.progressBar);
 mTrueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //is the user right? if pressing True button
            if(isMathProblemTrue == 1){
                //user is correct
                Toast.makeText(MainActivity.this,"Correct!",Toast.LENGTH_SHORT).show();
                generateMathProblem();
                mCountDownTimer.cancel();
                createNStartTimer();
            }else{
                //user is incorrect
                transferUserToStartScreen();
                mCountDownTimer.cancel(); // cancel
            }
        }
    });
}

 private void createNStartTimer() {
    mCountDownTimer = new CountDownTimer(timer_length,timer_interval) {
        @Override
        public void onTick(long millisUntilFinished) {
            int progress = (int) (millisUntilFinished/100);
            mProgressBar.setProgress(progress);
        }
        @Override
        public void onFinish() {
            mProgressBar.setProgress(0);
            transferUserToStartScreen();
        }
    }.start();
}

0 个答案:

没有答案