Android开发(五)——计时器

发送验证码后倒计时,Android自带计时器CountDownTimer,重写自己的计时器以实现跟新View的效果。

package com.lgaoxiao.widget;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * TextView的计时器
 * @author TimZhang
 *
 */
public class MyCountTimer extends CountDownTimer {
    private TextView btn;
    private int normalColor,timingColor;
    private String normalText;
    
    /**
     * @param millisInFuture
     * @param countDownInterval
     * @param v TextView
     * @param nColor Normal Color
     * @param tColor Timing Color
     */
    public MyCountTimer(long millisInFuture, long countDownInterval,TextView v,int nColor,int tColor) {
        super(millisInFuture, countDownInterval);
        this.btn = v;
        this.normalColor = nColor;
        this.timingColor = tColor;
        this.normalText = v.getText().toString();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        btn.setBackgroundColor(timingColor);
        btn.setEnabled(false);
        btn.setText(millisUntilFinished / 1000 + "s");
        
    }

    @Override
    public void onFinish() {
        btn.setEnabled(true);
        btn.setBackgroundColor(normalColor);
        btn.setText(normalText);
    }

}

参考:http://www.open-open.com/code/view/1426335036826

原文地址:https://www.cnblogs.com/ccdc/p/4431311.html