获取验证码倒计时功能

来源:互联网 发布:windows设置ntp服务器 编辑:程序博客网 时间:2024/05/16 04:47

自己的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中

@Overrideprotected void onCreate(Bundle savedInstanceState) {     bt_getcode = (TimerButton) findViewById(R.id.bt_getcode);            bt_getcode.onCreate();} @Overridepublic 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;    }}@Overrideprotected void onDestroy() {    super.onDestroy();    bt_getcode.onDestroy();}
0 0
原创粉丝点击