获取验证码倒计时功能

自己的app最近在做登录注册功能,有一个获取验证码验证手机的功能需要一个验证码倒计时控件,
之前公司项目中感觉实现方式有问题,就打算换种放心,网上搜索出来的都不满意,就想着之前实现过特卖倒计时的textview 就想着改一下。

效果如图,做到了退出当前页面再回来后时间继续接着倒计时

这里写图片描述
这里写图片描述
这里写图片描述

倒计时button代码

package com.hyp.lol.leagueoflegends.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

import com.hyp.lol.leagueoflegends.Constant;
import com.hyp.lol.leagueoflegends.MyApplication;

import java.util.HashMap;

/**
 * Created by Administrator on 2016/3/30.
 */
public class TimerButton extends Button implements Runnable {
    private long msecond;//秒
    private boolean run=false; //是否启动了
    private long endTime;

    public TimerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setTimes(long times) {
        msecond = times;
        endTime = System.currentTimeMillis()+60*1000;
    }
    /**
     *
     * 倒计时计算
     */
    private void ComputeTime()
    {
        msecond--;
        if(msecond==0)
        {
            stopRun();
        }
    }
    public boolean isRun() {
        return run;
    }
    public void beginRun() {
        this.run = true;
        run();
    }
    public void stopRun(){
        this.run = false;
    }
    @Override
    public void run() {
        //标示已经启动
        if(run) {
            ComputeTime();
            String strTime= msecond+"秒";
            this.setText(strTime);
            this.setEnabled(false);
            postDelayed(this, 1000);
        }else {
            String des= "获取验证码";
            setText(des);
            this.setEnabled(true);
            removeCallbacks(this);
        }
    }
    public void onCreate(){
        long nowTime = System.currentTimeMillis();
        if(MyApplication.registerCodeTimeMap!=null) {
            endTime = MyApplication.registerCodeTimeMap.get(Constant.CODE_ENDTIME);
            long timer = endTime - nowTime;
            if (timer > 0) {
                msecond = timer / 1000;
                beginRun();
            }
        }
    }
    public void onDestroy(){
        //activity退出后保存时间
        if(run){
            if (MyApplication.registerCodeTimeMap == null)
                MyApplication.registerCodeTimeMap = new HashMap<String, Long>();
            MyApplication.registerCodeTimeMap.put(Constant.CODE_ENDTIME, endTime);
        }
    }
}

MyApplication中

    public static Map<String, Long> registerCodeTimeMap;//用来存放倒计时的时间

xml中使用

    <com.hyp.lol.leagueoflegends.view.TimerButton
                android:id="@+id/bt_getcode"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="获取验证码"
                android:background="@drawable/code_button"
                />

其中background用到的drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <shape>
            <!-- 填充 #FF9455-->
            <solid android:color="@color/new_black" />
            <!-- 圆角 -->
            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
            <!-- 设置四个角的半径 -->

        </shape>

    </item>
    <item android:state_enabled="true">
        <shape>
            <!-- 填充 #FF9455-->
            <solid android:color="@color/darkslategray" />
            <!-- 圆角 -->
            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
            <!-- 设置四个角的半径 -->

        </shape>
    </item>

</selector>

Activity中

@Override
protected void onCreate(Bundle savedInstanceState) {
     bt_getcode = (TimerButton) findViewById(R.id.bt_getcode);
            bt_getcode.onCreate();
}
 @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bt_getcode:
            if(!bt_getcode.isRun()){
                bt_getcode.setTimes(CountdownTime);//自定义的时间 60
                bt_getcode.beginRun();
            }
            break;
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
    bt_getcode.onDestroy();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值