android——倒计时实现

时间:2021-05-05 10:14:30
 /** * 倒计时 */
    private class MyTime extends CountDownTimer {
        public MyTime(long millisInFuture, long countDownInterval)
        {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish()
        {
            tvCodeSend.setClickable(true);
            tvCodeSend.setText(getString(R.string.register_code_sms_get));
            tvCodeSend.setTextColor(getResources().getColor(R.color.register_code_get_sms));
        }

        @Override
        public void onTick(long millisUntilFinished)
        {
            tvCodeSend.setClickable(false);
            tvCodeSend.setText(getResources().getString(R.string.register_code_second,
                    (millisUntilFinished / 1000)));
            tvCodeSend.setTextColor(getResources().getColor(R.color.register_text));
        }
    }

启动

if (myTime != null)
        {
            myTime.cancel();
            myTime = null;
        }
   myTime = new MyTime(Constants.Countdown_start, Constants.Countdown_end);
        myTime.start();