how to get value from countdowntimer and display into toast

。_饼干妹妹 提交于 2019-12-12 06:46:40

问题


i have countdown timer from 1 to 9999 if i click start button the count will start, but if click stop button i need to get current value from countdown and display that value in toast but the countdown could not stop if i click stop button please help me

 private CountDownTimer countDownTimer;
         private boolean timerHasStarted = false;
         private Button startB;
         public TextView ;


         private final long startTime = 9999 * 1;

         private final long interval = 1 *1 ;

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

              startB = (Button) this.findViewById(R.id.button);
              startB.setOnClickListener(this);
              text = (TextView) this.findViewById(R.id.timer);
              countDownTimer = new MyCountDownTimer(startTime, interval);
              text.setText(text.getText() + String.valueOf(startTime / 1));
    }

    public void onClick(View v) {
          if (!timerHasStarted) {
           countDownTimer.start();
           timerHasStarted = true;
           startB.setText("STOP");
          } else {
           /*countDownTimer.cancel();
           timerHasStarted = false;
           startB.setText("RESTART");*/
          }
    }

    public class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);

        }

        @Override
        public void onFinish() {

            //text.setText("Time's up!");
            countDownTimer.start();

        }
        @Override
        public void onTick(long millisUntilFinished) {

             text.setText("" + millisUntilFinished / 1);

        }       
    }

thank you


回答1:


Here is my countdown timer:

QuestionCountdownTimer

public class QuestionCountdownTimer extends CountDownTimer {

private TextView remainingTimeDisplay;
private Context context;

public QuestionCountdownTimer(Context context,long millisInFuture, long countDownInterval,TextView remainingTimeDisplay) {
   super(millisInFuture, countDownInterval);
    this.context = context;
   this.remainingTimeDisplay = remainingTimeDisplay;
}

@Override
public void onTick(long millisUntilFinished) {
   long millis = millisUntilFinished;
   String hms = String.format("%02d:%02d",
           TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
           TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
   remainingTimeDisplay.setText(hms);
}

@Override
public void onFinish() {
   Toast.makeText(context,"COUNTDOWN FINISH :)",Toast.LENGTH_SHORT).show();
}

}

Note: TextView remainingTimeDisplay

remainingTimeDisplay.setText(hms); I use it to display the remaining time using a TextView

Here I call the timer:

//Start Quiz timer
QuestionCountdownTimer timer = new QuestionCountdownTimer(this,10000, 1000, remainingTimeDisplay);
    timer.start();

-first parameter: this - I use it for context to show Toast message

-second parameter: 10000 - total time (10 sec)

-third parameter: 1000 - countdown interval (1 sec)

-last parameter: dispaly remaining time in real time

Tested and working




回答2:


Create your CountDownTimer like this:

public class MyCountDownTimer extends CountDownTimer
    {
        private long timePassed = 0;

        public MyCountDownTimer(long startTime, long interval)
        {
            super(startTime, interval);
        }

        @Override
        public void onFinish()
        {
            //text.setText("Time's up!");
            countDownTimer.start();
        }

        @Override
        public void onTick(long millisUntilFinished)
        {
            timePassed++;
            text.setText("" + millisUntilFinished / 1);
        }

        public long getTimePassed()
        {
            return timePassed;
        }
    }

And on your onClick just do:

((MyCoundDownTimer) countDownTimer).getTimePassed();

to retrieve the time and set your textview text to it.




回答3:


You should use handler

private Handler tickResponseHandler = new Handler() {
    public void handleMessage(Message msg) {
        int time = msg.what;
        //make toast or do what you want
    }
}

and pass it to MyCountDownTimer constructor

private Handler handler;

public MyCountDownTimer(long startTime, long interval, Handler handler) {
        super(startTime, interval);
        this.handler = handler;
}

And send message

@Override
public void onTick(long millisUntilFinished) {
    text.setText("" + millisUntilFinished / 1);
    Message msg = new Message();
    msg.what = millisUntilFinished/1;
    handler.sendMessage(msg);
}     

That's all you need to do :)



来源:https://stackoverflow.com/questions/26338082/how-to-get-value-from-countdowntimer-and-display-into-toast

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