Android Studio上的计时器问题

时间:2017-08-10 06:38:47

标签: java android timer countdowntimer

我在测验游戏中遇到了问题。基本上它是一个多选游戏,玩家在每个问题上都有时间。当应用程序启动并且玩家看到第一个问题时,我有计时器启动。我的问题是,如果玩家正确回答问题或者错误地定时器开始给出随机值,即使我在onclick方法上将计时器重置为30秒。如何让计时器在30秒开始并正常倒计时。

public class MainActivity extends AppCompatActivity {

    //Views
    TextView questionTextView;
    TextView mscoreTextView;
    TextView mtimerTextView;
    Button mchoice1;
    Button mchoice2;
    Button mchoice3;
    Button mchoice4;


    //Constructors
    private questions Question = new questions();
    private Answers cAnswers = new Answers();
    private choices Choices = new choices();

    //Variables
    private int questionNumber = 0;
    private int mScore = 0;
    private String correctAnswer;


    public void onClick(View view) {

        Button answer1 = (Button) view;

        if(answer1.getText() == correctAnswer) {

            mScore = mScore + 1;
            Toast.makeText(getApplicationContext(), "CORRECT!!", Toast.LENGTH_SHORT).show();
            mtimerTextView.setText("30s");
            runTimer();

        } else {

            Toast.makeText(getApplicationContext(), "WRONG!!", Toast.LENGTH_SHORT).show();
            mtimerTextView.setText("30s");
            runTimer();

        }

        updateScore(mScore);
        updateUI();
    }


    private void updateScore(int points) {

        mscoreTextView.setText("" + points + "/" + Question.getLength());
    }


    public void runTimer() {

        new CountDownTimer(30100, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {

                String tick = String.valueOf(millisUntilFinished/1000 + "s");
                mtimerTextView.setText(tick);
            }

            @Override
            public void onFinish() {

                Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show();
                mtimerTextView.setText("0s");
                updateUI();
            }
        }.start();


    }


    private void updateUI () {

        if (questionNumber < Question.getLength()) {
            questionTextView.setText(Question.getQuestion(questionNumber));
            mchoice1.setText(Choices.getChoices(questionNumber, 1));
            mchoice2.setText(Choices.getChoices(questionNumber, 2));
            mchoice3.setText(Choices.getChoices(questionNumber, 3));
            mchoice4.setText(Choices.getChoices(questionNumber, 4));
            correctAnswer = cAnswers.getAnswer(questionNumber);

            questionNumber ++;

        } else {


            Toast.makeText(getApplicationContext(), "This is the last question", Toast.LENGTH_LONG).show();
            //Intent intent = new Intent(MainActivity.this, HighScoreActivity.class);
            //intent.putExtra("Score", mScore);
            //startActivity(intent);


        }

        runTimer();
    }

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

        questionTextView = (TextView) findViewById(R.id.questionTextView);
        mchoice1 = (Button) findViewById(R.id.choice1);
        mchoice2 = (Button) findViewById(R.id.choice2);
        mchoice3 = (Button) findViewById(R.id.choice3);
        mchoice4 = (Button) findViewById(R.id.choice4);


        mtimerTextView = (TextView) findViewById(R.id.timerTextView);
        mscoreTextView = (TextView) findViewById(R.id.scoreTextView);

        updateScore(mScore);
        updateUI();
    }
}

2 个答案:

答案 0 :(得分:1)

您必须在CountDownTimer类中定义变量。

public void runTimer() {

        new CountDownTimer(30100, 1000) {
        private int time = 30;

            @Override
            public void onTick(long millisUntilFinished) {
                mtimerTextView.setText(time--+"s");
            }

            @Override
            public void onFinish() {

                Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show();
                mtimerTextView.setText("0s");
                updateUI();
            }
        }.start();


    }

可取消定时器

如果您希望Timer可取消,则必须将其定义为全局变量。

private CountDownTimer timer; // global variable

通过调用以下runTimer()方法启动计时器。

public void runTimer() {

   timer = new CountDownTimer(30100, 1000) {
        private int time = 30;

            @Override
            public void onTick(long millisUntilFinished) {
                mtimerTextView.setText(time--+"s");
            }

            @Override
            public void onFinish() {

                Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show();
                mtimerTextView.setText("0s");
                updateUI();
            }
        }.start();


    }

您可以通过调用以下方法取消计时器。

public void stopTimer(){
   if(timer != null){
    timer.cancel();
  }
}

希望这会有所帮助

答案 1 :(得分:1)

问题是,你从未真正取消你推出的计时器。除此之外,每次你需要一个计时器 - 你创建一个新计时器,这不是必需的。以下必须解决您的问题:

您需要将CountDownTimer存储在类字段中:

private CountDownTimer timer;

然后你可以在应用程序开始时创建一次:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    ...
    timer = createTimer();
    ...
}

CreateTimer函数:

public void createTimer() {
    timer = new CountDownTimer(30100, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            ...
        }
        @Override
        public void onFinish() {
            ...
        }
}

}

因此,当您需要运行计时器时,您只需致电:

timer.start();

当用户给出答案时,您需要先取消计时器,然后再次启动:

public void onClick(View view) {
    ...
    timer.cancel();
    timer.start();
    ...
}

另外:您的OnClick()方法中有一些重复的代码。无论用户的答案是否正确,您都需要运行计时器并将值设置为mtimerTextView,因此基本上您希望在if-else构造之外执行此操作。