一行代码实现获取验证码倒计时功能 TimeCount

倒计时功能在开发中还是比较常见的一个功能,手机号登陆、注册,等页面常用,不知道大家用的什么方式来实现的,现在我把我的实现方式分享给大家

效果图:

使用方式:

TimeCount.getInstance(60000, 1000, tv_send, this).start();

源码:


/**
 * 倒计时功能类
 * @author zhangyuhua
 *
 *需要注意:
 *		1 控件必须使用 TextView (也可以使用其他的 需要将本类中的TextView 替换掉)
 *		2 onFinish()中 的background 需要图片
 *使用方式 如:TimeCount.getInstance(60000, 1000, tv_send, this).start();
 */
@SuppressLint("NewApi")
public  class TimeCount extends CountDownTimer {
	public static TimeCount time;  
	private Context context; //上下文
	private TextView textView; // 点击获取验证码的按钮

	public TimeCount(long millisInFuture, long countDownInterval,TextView textView,Context context) {
		super(millisInFuture, countDownInterval);
		this.context= context;
		this.textView=textView;

	}
	@Override
	public  void onTick(long millisUntilFinished){
		textView.setClickable(false);
		//设置背景颜色 这里用的是shape-
		textView.setBackground(context.getResources().getDrawable(R.drawable.shape_tv_bg_green));
		textView.setText(millisUntilFinished /1000+"s ");
		
	}

	@Override
	public  void onFinish(){

			textView.setText("重新获取验证码");
			textView.setClickable(true);
			//设置背景颜色 这里用的是shape
			textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_tv_bg_green));

	}

//单例
	public synchronized static TimeCount getInstance(long millisInFuture, long countDownInterval,TextView textView,Context context){
		if(time==null){
			time=new TimeCount(millisInFuture, countDownInterval, textView, context);
		}
		return time;
	}
	
}

Drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 实心 -->
            <solid android:color="@color/border_gray"/>
            <!-- 描边 -->
            <stroke
                android:width="0dp" />
            <!-- 圆角 -->
            <corners
                android:radius="10dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />

</shape>

简单讲解:

用起来挺方便的,使用过程中会有个小bug : 返回上一个界面后再进入注册页面时点击发送验证码时 ,无任何改变。

这个小bug也简单。因为CountDownTimer 的cancel 在androidStudio中实效了,怎么办?;

经过多次测试发现,在活动Activity结束前 调用一下TimeCount的onFinish() 方法后该功能一切可以正常。那么我们可以做如下操作,

1、在 TimeCount中 添加一个状态值 status ,默认等于0,在onFinish(),方法中进行判断 

@Override
	public  void onFinish(){

		if (status==0){
			textView.setText("重新获取验证码");
		}else {
			textView.setText("发送验证码");
		}
			textView.setClickable(true);
		//设置背景颜色 这里用的是shape  假如美工给出的背景色有两种是在这里进行设置(可点击色)
			textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_tv_bg_green));

	}

2、在TimeCount中添加一个 充值方法 resetting();

/**
	 * 当计时器未结束时页面突然关闭时调用此方法
	 */
	public void resetting(){
		this.status=1;
		onFinish();
		cancel();
	}

resetting() 方法调用之后这个Bug就可以解决了。

 

完整代

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;


import test.zyh.cn.com.testandroid.R;

/**
 * 倒计时功能类
 * @author zhangyuhua
 *
 *需要注意:
 *		1 控件必须使用 TextView (也可以使用其他的 需要将本类中的TextView 替换掉)
 *		2 onFinish()中 的background 需要图片
 *使用方式 如:TimeCount.getInstance(60000, 1000, tv_send, this).start();
 */
@SuppressLint("NewApi")
public  class TimeCount extends CountDownTimer {
	public static TimeCount time;
	private Context context;
	private TextView textView;

	private  int status = 0; //当status == 0的时候是正常状态,其他值为非正常

	public TimeCount(long millisInFuture, long countDownInterval,TextView textView,Context context) {
		super(millisInFuture, countDownInterval);
		this.context= context;
		this.textView=textView;

	}
	@Override
	public  void onTick(long millisUntilFinished){
		textView.setClickable(false);
		//设置背景颜色 这里用的是shape  假如美工给出的背景色有两种是在这里进行设置(不可点击色)
		textView.setBackground(context.getResources().getDrawable(R.drawable.shape_tv_bg_green));
		textView.setText(millisUntilFinished /1000+"s ");
		
	}

	@Override
	public  void onFinish(){

		if (status==0){
			textView.setText("重新获取验证码");
		}else {
			textView.setText("发送验证码");
		}
			textView.setClickable(true);
		//设置背景颜色 这里用的是shape  假如美工给出的背景色有两种是在这里进行设置(可点击色)
			textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_tv_bg_green));

	}

	public synchronized static TimeCount getInstance(long millisInFuture, long countDownInterval,TextView textView,Context context){
		if(time==null){
			time=new TimeCount(millisInFuture, countDownInterval, textView, context);
		}
		return time;
	}

	/**
	 * 当计时器未结束时页面突然关闭时调用此方法
	 */
	public void resetting(){
		this.status=1;
		onFinish();
		cancel();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值