源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android获取验证码倒计时显示效果

  • 时间:2022-04-16 17:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android获取验证码倒计时显示效果
前面为大家讲过计时器的顺时针的两种方法,在录制视频等操作中颇有使用,今天就给大家带来倒计时实现的两种方式。 虽然最近写的都比较简单和基础,不过简单不代表熟悉,基础不代表就会,大牛绕过,哈,中牛小牛也可以绕过,这个是写给初学者的。 先搞个效果图。 [img]http://files.jb51.net/file_images/article/201610/20161012102010540.gif?2016912102017[/img] 代码实现方式也超级简单啦,这里首推第一种实现方式,而且也是比较适合大家的,就是通过直接继承CountDownTimer来实现。 对于CountDownTimer这个类很简单,继承它的时候必须重写构造方法和实现其虚拟方法。 构造方法的两个参数分别是(倒计时开始时间,间隔时间) 另外两个方法分别是onTick(现在还剩的时间),计时结束后你想做的时间可以在onFinish()中做。 值的注意的是,所有的时间都是以毫秒形式来做的,所以在你使用的时候要记得整除1000取商。 不过由于我使用的是私有内部类的方式对外部类存在引用,为了防止内存泄漏,在Activity销毁的时候应该注意对其置空,同样我们也应该避免重复创建对象。 另外一种方式还是使用我们常用的Handler + Thread的方式来实现。不过实现的时候同样要非常小心内存泄漏,因为如果用户在销毁Activity的时候应该注意让其计时子线程不再循环,这个可以通过设置一个tag标签对其判断。 这样在销毁的时候把这个tag标签置为false,结束线程的执行! 下面是实现代码:
package com.example.nanchen.timerdemo;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

 private Button mBtnGetCode;
 private TimeCount mTimeCount;
 private Button mBtnGetCode2;
 private boolean timeFlag = true;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 mBtnGetCode = (Button) findViewById(R.id.main_btn_get_code);
 mBtnGetCode.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  mTimeCount = null;
  mTimeCount = new TimeCount(60 * 1000, 1000);
  mTimeCount.start();
  }
 });

 mBtnGetCode2 = (Button) findViewById(R.id.main_btn_get_code_2);
 mBtnGetCode2.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  mBtnGetCode2.setClickable(false);
  mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.btn_unable));
  timeFlag = true;
  new Thread() {
   @Override
   public void run() {
   super.run();
   for (int i = 59; i >= 0 && timeFlag; i--) {
    try {
    sleep(1000);
    Message msg = Message.obtain();
    msg.what = i;
    mHandler.sendMessage(msg);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
   }
   }
  }.start();
  }
 });
 }

 private Handler mHandler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
  super.handleMessage(msg);
  if (msg.what > 0) {
  mBtnGetCode2.setText("(" + msg.what + ")秒后重试");
  } else {
  mBtnGetCode2.setText("获取验证码");
  mBtnGetCode2.setClickable(true);
  mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
  }
 }


 };


 /**
 * Activity 销毁的时候注意把所有引用置为空,防止内存泄漏
 */
 @Override
 protected void onDestroy() {
 super.onDestroy();
 mTimeCount = null;
 timeFlag = false;
 }

 /**
 * 实现倒计时的类
 */
 private class TimeCount extends CountDownTimer {

 /**
  * @param millisInFuture The number of millis in the future from the call
  *    to {@link #start()} until the countdown is done and {@link #onFinish()}
  *    is called.
  * @param countDownInterval The interval along the way to receive
  *    {@link #onTick(long)} callbacks.
  */
 public TimeCount(long millisInFuture, long countDownInterval) {
  super(millisInFuture, countDownInterval);
 }

 /**
  * 计时过程显示 按钮不可用 设置为灰色
  *
  * @param millisUntilFinished
  */
 @Override
 public void onTick(long millisUntilFinished) {
  mBtnGetCode.setClickable(false);
  mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.btn_unable));
  mBtnGetCode.setText("(" + millisUntilFinished / 1000 + ")秒后重试");
 }

 /**
  * 计时结束调用
  */
 @Override
 public void onFinish() {
  mBtnGetCode.setClickable(true);
  mBtnGetCode.setText("获取验证码方式1");
  mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
 }
 }


}
简单看一下xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.nanchen.timerdemo.MainActivity">

 <Button
 android:layout_marginTop="10dp"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/main_btn_get_code"
 android:text="获取验证码方式1"
 android:background="@color/colorPrimaryDark"/>

 <TextView
 android:layout_width="match_parent"
 android:layout_height="1dp"
 android:id="@+id/main_line1"
 android:background="@color/btn_unable"
 android:layout_below="@+id/main_btn_get_code"
 android:layout_marginTop="10dp"/>

 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/main_line1"
 android:layout_marginTop="10dp"
 android:text="获取验证码方式2"
 android:id="@+id/main_btn_get_code_2"
 android:background="@color/colorAccent"/>

</RelativeLayout>
写在最后:虽然代码和实现都非常简单,你可能不费吹灰之力,不过倘若转载的话,还是留个本文链接吧~thank you! github链接:[url=https://github.com/nanchen2251/TimerDemo]https://github.com/nanchen2251/TimerDemo[/url]  以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部