Android change time of countDownTimer

♀尐吖头ヾ 提交于 2019-12-14 02:36:38

问题


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.



来源:https://stackoverflow.com/questions/11177379/android-change-time-of-countdowntimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!