获取验证码倒计时功能
来源:互联网 发布: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
- 获取验证码倒计时功能
- Handler实现验证码获取倒计时功能
- iOS-获取验证码中的倒计时功能
- 实现获取验证码倒计时功能
- android获取验证码倒计时功能
- android 获取短信验证码倒计时功能
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 倒计时获取验证码
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- Android实现获取验证码的倒计时功能
- 个人笔记 html 06 获取验证码之后倒计时功能
- hdu 1754 I Hate It(线段树单点更新求区间最大值)
- 【图像处理】FFmpeg-0
- 高通 MSM8K bootloader 之三: LK
- Jquery ajax 请求容易出现的问题http415,http400
- MySql 中IFNULL、ISNULL和NULLIF的区别
- 获取验证码倒计时功能
- MYSQL级联条件删除数据
- 高通 MSM8K bootloader 之四: ramdump
- 高可用&负载均衡常用架构
- struts2文件上传获取文件名、文件类型
- Go net包介绍
- SQL server 中convert 函数转换日期格式
- Levenberg-Marquardt(LM算法)
- linux下环境变量、启动脚本、进程判断设置