注册用户倒计时

/**
 * 倒计时
 */
public class TimeModel {

    private long millsecond;
    private long count = 0;// 剩余时间
    private CountDownTimer countTime;

    private Button button;

    public TimeModel(Button btn, long millsec) {
        this.millsecond = millsec;
        this.button = btn;
        countTime = new CountDownTimer(millsecond, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                count = millisUntilFinished;
                button.setText(button.getContext().getString(R.string.seconds_resent, millisUntilFinished / 1000));
            }

            @Override
            public void onFinish() {
                button.setEnabled(true);
                button.setBackgroundResource(R.drawable.blue_border_bg);
                button.setText("重新发送");
            }
        };
    }

    private Activity act;
    private TextView tv;
    private long show;

    /**
     * 延时返回主界面
     *
     * @param activity
     * @param textView
     * @param sec
     * @param showTime
     */
    public TimeModel(Activity activity, TextView textView, long sec, long showTime) {
        this.millsecond = sec;
        this.act = activity;
        this.tv = textView;
        this.show = showTime;
        countTime = new CountDownTimer(millsecond, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                count = millisUntilFinished;
                if (millisUntilFinished <= show) {
                    tv.setVisibility(View.VISIBLE);
                    tv.setText("倒计时:" + millisUntilFinished / 1000);
                } else {
                    tv.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFinish() {
            }
        };
    }

    public TimeModel(Activity activity, long sec, long showTime) {
        this.millsecond = sec;
        this.act = activity;
        this.show = showTime;
        countTime = new CountDownTimer(millsecond, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                count = millisUntilFinished;
            }

            @Override
            public void onFinish() {
            }
        };
    }

    public void startTime() {
        if (button != null) {
            button.setEnabled(false);
        }
        countTime.start();
    }

    public void RestartTimeCount() {
        // LogUtil.debug("TimeModel", "RestartTimeCount1"+millsecond+" "+count);
        if (millsecond - count > 2000) {
            // LogUtil.debug("TimeModel", "RestartTimeCount2"+millsecond+" "+count);
            countTime.cancel();
            startTime();
        }
    }

    public void endTime() {
        countTime.cancel();
        // countTime=null;
    }
}

 

posted @ 2016-09-02 10:45  心夢無痕  阅读(206)  评论(0编辑  收藏  举报