Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android获取验证码倒计时显示效果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

前面为大家讲过计时器的顺时针的两种方法,在录制视频等操作中颇有使用,今天就给大家带来倒计时实现的两种方式。

然最近写的都比较简单和基础,不过简单不代表熟悉,基础不代表就会,大牛绕过,哈,中牛小牛也可以绕过,这个是写给初学者的。

先搞个效果图。

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

代码实现方式也超级简单啦,这里首推第一种实现方式,而且也是比较适合大家的,就是通过直接继承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://scheR_977_11845@as.android.com/apk/res/android"
 xmlns:tools="http://scheR_977_11845@as.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链接https://github.com/nanchen2251/TimerDemo

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android获取验证码倒计时显示效果全部内容,希望文章能够帮你解决Android获取验证码倒计时显示效果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。