android 倒计时管理类

今天闲来没什么事儿,突然看到倒计时的功能,就像着给封装出来,以便以后的调用,使用起来大概就是这么用的

//获得Manager对象的管理类,mButton为倒计时按钮

CountdownManager  countdownManager = new CountdownManager(mButton, 120 * 1000, 0, 1000);
//设置开始倒计时之前的文字,结束的文字,倒计时中的文字,$符在运行中会被替换成倒计时的秒数

countdownManager.setStateStr("点击获取验证码", "再次获取验证码", "$秒");

//调用此方法停止倒计时

countdownManager.cuntdownStop();


废话不多说了,底下就是咱们的代码片段了


package com.util;



import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class CountdownManager {
/** 倒计时停止的文字 */
private String stopState = "倒计时结束";
/** 倒计时开始之前的文字 */
private String startState = "开始倒计时";
/** 倒计时进行中的文字 */
private String runState = "$秒";
/**
* 操作的View
*/
private View view;
/**
* 总长度
*/
private long startSeconds = -1;
/** 停止的时间 */
private long stopSeconds = -1;
/** 当前时间 */
private long currentTime = 0;
/**
* 时间间隔,毫秒
*/
private long timeInterval = -1;
/**
* 是否为自增
*/
private boolean isAdd = false;
/** 是否停止 true为已经停止,false为没有停止 */
private boolean isStop = true;
/** 倒计时回调接口 */
private CountdownInterface countdownInterface;


/**
* @param view
*            操作的View
* @param startSeconds
*            开始时间
* @param stopSeconds
*            停止的时间
* @param timeInterval
*            时间间隔
*/
public CountdownManager(View view, long startSeconds, long stopSeconds, long timeInterval) {
this.view = view;
this.startSeconds = startSeconds;
this.stopSeconds = stopSeconds;
this.timeInterval = timeInterval;
if (countdownInterface == null) {
countdownInterface = new DefCountdownInterface();
}
}


/**
* 停止倒计时
*/
public void cuntdownStop() {
this.isStop = true;
}


/**
* 设置自定义文字

* @param startState
*            开始的文字
* @param stopState
*            结束的文字
* @param runState
*            运行中的文字 $ 代表时间,如$秒,代表1秒,2秒,3秒
*/
public void setStateStr(String startState, String stopState, String runState) {
this.stopState = stopState;
this.startState = startState;
this.runState = runState;
view.post(new Runnable() {
@Override
public void run() {
if (countdownInterface != null) {
countdownInterface.startCountDown();
}
}
});
}


/**
* @param view
*            操作的View
*/
public CountdownManager(View view) {
this.view = view;
if (countdownInterface == null) {
countdownInterface = new DefCountdownInterface();
}
}


/**
* 初始化数据
*/
private void init() {
if (startSeconds == -1 || timeInterval == -1) {
if (isAdd) {// 增
startSeconds = 0;
stopSeconds = 60 * 1000;
timeInterval = 1000;
} else {// 减
startSeconds = 60 * 1000;
stopSeconds = 0;
timeInterval = 1000;
}
}
currentTime = startSeconds;
// 重置倒计时状态
isStop = false;
}


/** 获取当前状态 true为倒计时已经停止,false为倒计时没有停止 */
public boolean isStop() {
return isStop;
}


/**
* 启动
*/
public void start() {
if (!isStop) {// 为false说明正在执行倒计时,直接return这个事件
return;
}
if (view == null) {
Log.e("CountdownManager", "view is null");
return;
}
init();
if (countdownInterface != null) {
countdownInterface.startCountDown();
countdownInterface.countDown(currentTime / timeInterval);
}
runCountDown();
}


/**
* 执行倒计时
*/
private void runCountDown() {
view.postDelayed(new Runnable() {
@Override
public void run() {
currentTime = currentTime - timeInterval;
if (isAdd) {
if (currentTime > stopSeconds) {
isStop = true;
}
} else {
if (currentTime < stopSeconds) {
isStop = true;
}
}
if (isStop) {
if (countdownInterface != null) {
countdownInterface.countDown(currentTime / timeInterval);
countdownInterface.stopCountDown();
}
isStop = true;// 停止倒计时
return;
} else {
runCountDown();
if (countdownInterface != null) {
countdownInterface.countDown(currentTime / timeInterval);
}
}
}
}, timeInterval);
}


private class DefCountdownInterface implements CountdownInterface {
@Override
public void countDown(long time) {
if (view != null && view instanceof TextView && runState != null) {
((TextView) view).setText(runState.replace("$", time + ""));
}
}


@Override
public void startCountDown() {
if (view != null && view instanceof TextView && runState != null) {
((TextView) view).setText(startState);
}
}


@Override
public void stopCountDown() {
if (view != null && view instanceof TextView && runState != null) {
((TextView) view).setText(stopState);
}
}
}


/**
* 倒计时回调

* @param countdownInterface
*/
public void setCountdownInterface(CountdownInterface countdownInterface) {
this.countdownInterface = countdownInterface;
}


public interface CountdownInterface {
/** 当前倒计时到什么时间 */
void countDown(long time);


/** 开始倒计时 */
void startCountDown();


/** 停止倒计时 */
void stopCountDown();
}

}




好的话点个赞

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值