获取验证码倒计时功能
2016-03-30 14:17
381 查看
自己的app最近在做登录注册功能,有一个获取验证码验证手机的功能需要一个验证码倒计时控件,
之前公司项目中感觉实现方式有问题,就打算换种放心,网上搜索出来的都不满意,就想着之前实现过特卖倒计时的textview 就想着改一下。
效果如图,做到了退出当前页面再回来后时间继续接着倒计时
倒计时button代码
MyApplication中
xml中使用
其中background用到的drawable
Activity中
之前公司项目中感觉实现方式有问题,就打算换种放心,网上搜索出来的都不满意,就想着之前实现过特卖倒计时的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(); }
相关文章推荐
- java Socket编程初探_TCP
- hdu 1754 I Hate It(线段树单点更新求区间最大值)
- MYSQL级联条件删除数据
- HTTP/1.1
- IOS开发-UI学习-NSBundle和NSURL的区别(读取文件以及写入文件)
- 高通 MSM8K bootloader 之三: LK
- 【图像处理】FFmpeg-0
- exists in SQL
- 网页中JS函数自动执行常用三种方法
- 高通 MSM8K bootloader 之三: LK
- Java 多线程线程池 - 线程池原理
- Linux启动流程
- windows中mac虚拟机网络连接
- 通过反射调用动态调用方法
- Html+CSS input type=file 文件标签的美化
- lightoj 1125 - Divisible Group Sums 01背包变形
- 图的搜索(bfs)
- BOE 不能登录问题
- 学生如何提高专业英文阅读能力--施一公教授
- hadoop安装