Android change time of countDownTimer

2019-08-12 16:08发布

问题:

my application is questioning/answering application, client/server, the client is android application, and the server is java tomcat server application

i want the user to answer any question in 25 seconds, but i will give him time to read the question, that time depends on the number of words the question has, for example if the question has 10 words i will give him 10 seconds to read the question and then 25 seconds to answer, for sure the user who answers the question in 10seconds(of the 25 seconds) will take more marks that the user who answers the same question in 20seconds

so my problems is how to edit dynamickly the time of the countDownTimer

showHint.setText("Show Hint 2");
//here i have to edit
timeToDirectAnswer.start();

i know that there is a way to do that by initializing the timer with the dynamic time, but i don't want to do that because my application depends on workflow and that flow may exchange, so the code will be so sophisticated and hard to edit

maybe another way is to make the countDownTimer sleeps for exact seconds but in this way i have to build another timer to tell user how many times remains to begin counting marks

回答1:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

This is the constructor definition:

public CountDownTimer (long millisInFuture, long countDownInterval)

Since: API Level 1

Parameters

millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.

countDownInterval : The interval along the way to receive onTick(long) callbacks.

EDIT:

You will have to put the question in String variable, then split this String with (space) in an Array, and the array count would be the number of words of that question.

Then, you will do an equation to calculate the countdown time for reading:

time=numberOfWords*2000;

Thats for 2 seconds each word.