Android验证码倒计时功能实现
2016-03-17 11:04
639 查看
1、首先,自定义一个验证码倒计时类,继承CountDownTimer
3、布局文件
项目源码地址
/** * 用于验证码倒计时 * @author Sunday * */ public class MyCountDownTime extends CountDownTimer { private Button btn; private String message; public MyCountDownTime(long millisInFuture, long countDownInterval, Button btn, String message) { super(millisInFuture, countDownInterval); this.btn=btn; this.message =message; // TODO Auto-generated constructor stub } @Override public void onFinish() { // TODO Auto-generated method stub btn.setEnabled(true); btn.setText(message); } @Override public void onTick(long arg0) { // TODO Auto-generated method stub btn.setEnabled(false); btn.setText("倒计时:(" + arg0 / 1000 + ")"); } }2、具体在代码中的用法如下
public class MainActivity extends BaseActivity implements OnClickListener { private EditText et_phone, et_smscode; private Button btn_send_smscode,btn_reset, btn_submit; private MyCountDownTime myCountDownTime;// 用于验证码倒计时 private Handler mHandler;//用于执行耗时操作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } /** * * @Description: TODO 初始化控件布局 * @author Sunday * @date 2016年3月16日 */ private void initView() { // TODO Auto-generated method stub et_phone = (EditText) findViewById(R.id.et_phone); et_smscode = (EditText) findViewById(R.id.et_smscode); btn_send_smscode = (Button) findViewById(R.id.btn_send_smscode); btn_reset = (Button) findViewById(R.id.btn_reset); btn_submit = (Button) findViewById(R.id.btn_submit); } /** * @Description: TODO 绑定监听事件 * @throws @author * Sunday * @date 2016年3月16日 */ private void setListener() { // TODO Auto-generated method stub btn_send_smscode.setOnClickListener(this); btn_submit.setOnClickListener(this); btn_reset.setOnClickListener(this); } /* *点击事件 */ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_send_smscode: getCode(); break; case R.id.btn_submit: Toasters("提交成功"); break; case R.id.btn_reset: cancelTimer(); Toasters("重置成功"); break; default: break; } } /** * * @Description: TODO 获取验证码,模拟网络访问耗时操作 * @author Sunday * @date 2016年3月16日 */ private void getCode(){ buildProgressDialog("获取验证码中,请稍等"); mHandler = new Handler(); mHandler.postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub cancelProgressDialog(); Toasters("获取验证码成功,稍后请查看手机信息"); startTimer(); } }, 2000); } /** * * @Description: TODO 发送成功后,开始倒计时 * @throws @author * Sunday * @date 2016年3月16日 */ private void startTimer() { if (null == myCountDownTime) { myCountDownTime = new MyCountDownTime(60000, 1000, btn_send_smscode, "重新发送"); } myCountDownTime.start(); } /** * * @Description: TODO 一般发送失败时,需要重置Button状态 * @author Sunday * @date 2016年3月16日 */ private void cancelTimer(){ if(null != myCountDownTime){ myCountDownTime.cancel(); myCountDownTime.onFinish(); } }
3、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eee" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="1" android:hint="请输入手机号码" android:inputType="number" android:maxLength="11" android:paddingLeft="8dp" /> <Button android:id="@+id/btn_send_smscode" android:layout_width="90dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:gravity="center" android:padding="4dp" android:text="获取验证码" android:textSize="15sp" > </Button> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <EditText android:id="@+id/et_smscode" android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="1" android:hint="有时验证码获取失败的时候需要使用" android:paddingLeft="8dp" /> <Button android:id="@+id/btn_reset" android:layout_width="90dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:gravity="center" android:padding="4dp" android:text="重置验证码" android:textSize="15sp" > </Button> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="45dp" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="提交" android:textSize="18sp" /> </RelativeLayout> </LinearLayout>
项目源码地址
相关文章推荐
- ANDROID安卓activity与fragment,activity之间互相传值(四)
- android环境搭建时出现各种问题时手动安装的方法
- android 通过eclipse混淆代码、签名
- android事件拦截处理机制详解
- 判断手机是否安装某程序的方法:
- android图像处理之Bitmap
- Android使用SharedPreferences实现登录帐号和密码的保存方法简介
- Android中控件TextView实现跑马灯只显示省略号问题
- android 6.0 修改vold check sd卡同步改成异步机制
- Android第三方登录详解1
- android日常干货
- Android内存泄漏分析及实践(一)
- 浅谈 RxAndroid + Retrofit + Databinding
- android之ThreadLocal详解
- Android---AlarmManager闹钟设置
- Android解决java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
- 7_Android动画深入分析
- Android WebView上实现JavaScript与Java交互
- ProgressDialog使用总结
- Android 数据绑定框架DataBinding,堪称解决界面逻辑的黑科技