使用

第一种方案:自定义控件

1.在布局中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"> <com.ywj.countdowntextviewdemo.CountDownTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countDownTextView"
android:text="Hello World!" />
</RelativeLayout>

2.在Activity中获取控件使用

  CountDownTextView countDownTextView = (CountDownTextView) findViewById(R.id.countDownTextView);
countDownTextView.setCountDownMillis();
countDownTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("MainActivity","点击事件");
}
});
countDownTextView.start();

3.CountDownTextView.java

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView; /**
* 倒计时TextView
* Created by weijing on 2017-08-21 14:43.
*/ public class CountDownTextView extends TextView { /**
* 提示文字
*/
private String mHintText = "重新发送"; /**
* 倒计时时间
*/
private long mCountDownMillis = 60_000; /**
* 剩余倒计时时间
*/
private long mLastMillis;
/**
* 间隔时间差(两次发送handler)
*/
private long mIntervalMillis = 1_000;
/**
* 开始倒计时code
*/
private final int MSG_WHAT_START = 10_010;
/**
* 可用状态下字体颜色Id
*/
private int usableColorId = android.R.color.holo_blue_light;
/**
* 不可用状态下字体颜色Id
*/
private int unusableColorId = android.R.color.darker_gray; public CountDownTextView(Context context) {
super(context);
} public CountDownTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public CountDownTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what) {
case MSG_WHAT_START:
// Log.e("l", mLastMillis + "");
if (mLastMillis > ) {
setUsable(false);
mLastMillis -= mIntervalMillis;
mHandler.sendEmptyMessageDelayed(MSG_WHAT_START, mIntervalMillis);
} else {
setUsable(true);
}
break;
}
}
}; /**
* 设置是否可用
*
* @param usable
*/
public void setUsable(boolean usable) { if (usable) {
//可用
if (!isClickable()) {
setClickable(usable);
setTextColor(getResources().getColor(usableColorId));
setText(mHintText);
}
} else {
//不可用
if (isClickable()) {
setClickable(usable);
setTextColor(getResources().getColor(unusableColorId));
}
setText(mLastMillis / + "秒后" + mHintText);
} } /**
* 设置倒计时颜色
*
* @param usableColorId 可用状态下的颜色
* @param unusableColorId 不可用状态下的颜色
*/
public void setCountDownColor(@ColorRes int usableColorId, @ColorRes int unusableColorId) {
this.usableColorId = usableColorId;
this.unusableColorId = unusableColorId;
} /**
* 设置倒计时时间
*
* @param millis 毫秒值
*/
public void setCountDownMillis(long millis) {
mCountDownMillis = millis;
} /**
* 开始倒计时
*/
public void start() {
mLastMillis = mCountDownMillis;
mHandler.sendEmptyMessage(MSG_WHAT_START);
} /**
* 重置倒计时
*/
public void reset() {
mLastMillis = ;
mHandler.sendEmptyMessage(MSG_WHAT_START);
} @Override
public void setOnClickListener(@Nullable final OnClickListener onClickListener) {
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mHandler.removeMessages(MSG_WHAT_START);
start();
onClickListener.onClick(v);
}
}); } @Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeMessages(MSG_WHAT_START);
}
}

第二种方案:工具类

1.在activity中使用

  TextView textView = (TextView) findViewById(R.id.textView);
new CountDownUtil(textView)
.setCountDownMillis(60_000L)//倒计时60000ms
.setCountDownColor(android.R.color.holo_blue_light,android.R.color.darker_gray)//不同状态字体颜色
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("MainActivity","发送成功");
}
})
.start();

2.CountDownUtil.java

import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView; import java.lang.ref.WeakReference; /**
* 倒计时工具
* Created by weijing on 2017-08-22 11:50.
*/ public class CountDownUtil {
/**
* 开始倒计时code
*/
private final int MSG_WHAT_START = 10_010;
/**
* 弱引用
*/
private WeakReference<TextView> mWeakReference;
/**
* 倒计时时间
*/
private long mCountDownMillis = 60_000;
/**
* 提示文字
*/
private String mHintText = "重新发送"; /**
* 剩余倒计时时间
*/
private long mLastMillis; /**
* 间隔时间差(两次发送handler)
*/
private long mIntervalMillis = 1_000; /**
* 可用状态下字体颜色Id
*/
private int usableColorId = android.R.color.holo_blue_light;
/**
* 不可用状态下字体颜色Id
*/
private int unusableColorId = android.R.color.darker_gray; private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what) {
case MSG_WHAT_START:
if (mLastMillis > ) {
setUsable(false);
mLastMillis -= mIntervalMillis;
if (mWeakReference.get() != null) {
mHandler.sendEmptyMessageDelayed(MSG_WHAT_START, mIntervalMillis);
}
} else {
setUsable(true);
}
break;
}
}
}; public CountDownUtil(TextView textView) {
mWeakReference = new WeakReference<>(textView);
} public CountDownUtil(TextView textView, long countDownMillis) {
mWeakReference = new WeakReference<>(textView);
this.mCountDownMillis = countDownMillis;
} public CountDownUtil setCountDownMillis(long countDownMillis) {
this.mCountDownMillis = countDownMillis;
return this;
} /**
* 设置是否可用
*
* @param usable
*/
private void setUsable(boolean usable) {
TextView mTextView = mWeakReference.get();
if (mTextView != null) {
if (usable) {
//可用
if (!mTextView.isClickable()) {
mTextView.setClickable(usable);
mTextView.setTextColor(mTextView.getResources().getColor(usableColorId));
mTextView.setText(mHintText);
}
} else {
//不可用
if (mTextView.isClickable()) {
mTextView.setClickable(usable);
mTextView.setTextColor(mTextView.getResources().getColor(unusableColorId));
}
String content = mLastMillis / + "秒后" + mHintText;
mTextView.setText(content); }
}
} /**
* 设置倒计时颜色
*
* @param usableColorId 可用状态下的颜色
* @param unusableColorId 不可用状态下的颜色
*/
public CountDownUtil setCountDownColor(@ColorRes int usableColorId, @ColorRes int unusableColorId) {
this.usableColorId = usableColorId;
this.unusableColorId = unusableColorId;
return this;
} /**
* 开始倒计时
*/
public CountDownUtil start() {
mLastMillis = mCountDownMillis;
mHandler.sendEmptyMessage(MSG_WHAT_START);
return this;
} public CountDownUtil setOnClickListener(@Nullable final View.OnClickListener onClickListener) {
TextView mTextView = mWeakReference.get();
if (mTextView != null)
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHandler.removeMessages(MSG_WHAT_START);
start();
onClickListener.onClick(v);
}
});
return this;
} /**
* 重置停止倒计时
*/
public CountDownUtil reset() {
mLastMillis = ;
mHandler.sendEmptyMessage(MSG_WHAT_START);
return this;
}

TextView textView = (TextView) findViewById(R.id.textView);
new CountDownUtil(textView)
.setCountDownMillis(60_000L)//倒计时60000ms
.setCountDownColor(android.R.color.holo_blue_light,android.R.color.darker_gray)//不同状态字体颜色
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("MainActivity","发送成功");
}
})
.start();

Android 验证码倒计时两种方案的更多相关文章

  1. android环境下两种md5加密方式

    在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...

  2. 详解Grunt插件之LiveReload实现页面自动刷新(两种方案)

    http://www.jb51.net/article/70415.htm    含Grunt系列教程 这篇文章主要通过两种方案详解Grunt插件之LiveReload实现页面自动刷新,需要的朋友可以 ...

  3. android studio gradle 两种更新方法更新

    android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...

  4. Xamarin Android Fragment的两种加载方式

    android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...

  5. Linux下实现秒级定时任务的两种方案

    Linux下实现秒级定时任务的两种方案(Crontab 每秒运行): 第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command s ...

  6. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  7. [转载]Java操作Excel文件的两种方案

    微软在桌面系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel.时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发.在我们实际开发企业办公系统的过程中,常常有客户这样子要 ...

  8. .Net Core下使用RabbitMQ比较完备的两种方案(虽然代码有点惨淡,不过我会完善)

    一.前言     上篇说给大家来写C#和Java的方案,最近工作也比较忙,迟到了一些,我先给大家补上C#的方案,另外如果没看我上篇博客的人最好看一下,否则你可能看的云里雾里的,这里我就不进行具体的方案 ...

  9. 比较好用的移动端适配的两种方案及flexible和px2rem-loader在webpack下的配置

    移动端适配,目前自己常用的两种 方案,参考以下两篇好文 方案一:使用lib-flexible包 https://www.w3cplus.com/mobile/lib-flexible-for-html ...

随机推荐

  1. 在IIS6.0以上版本发布Ajax中,解决添加.v路径找不到的问题?

    问题描述:配置Aiax方式如下: 1.在AppCode中加入文件夹Ajax,加入两个类文件: Ajax.cs: using System; using System.Collections.Gener ...

  2. hibernate 中的拦截器EmptyInterceptor接口功能

    Interceptor接口提供了从会话(session)回调(callback)应用程序(application)的机制, 这种回调机制可以允许应用程序在持久化对象被保存.更新.删除或是加载之前,检查 ...

  3. getElementsByName()获取标签时的注意

    var aDiv = document.getElementsByTagName('div');//获取的标签名注意你下面用的是哪一个div的标签名,例如 aDiv[0] 才可以: <!-- 注 ...

  4. Java编程环境IntelliJ IDEA

    1. 下载并安装jdk,进行配置 https://www.cnblogs.com/zhangchao0515/p/6806408.html 2. 下载并安装 IntelliJ IDEA, 并进行破解 ...

  5. Aaronson

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. PPJQR-GKK-2深度学习及应用

    参考资料: UFLDL:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial Deep Learning Turtorial:h ...

  7. 微信小程序开发之页面wxml里面实现循环 wx:for

    js代码: Page({ data:{ upploadimagelist:{},    //上报图片列表 js数组 }}) 后台数据库保存的格式:{"imageList":[{&q ...

  8. Angular6在自定义指令中使用@HostBingDing() 和@HostListener()

    emmm,,,最近在为项目的第二阶段铺路,偶然看到directive,想想看因为项目已经高度集成了第三方组件,所以对于自定义指令方面的经验自己实在知之甚少,后面经过阅读相关资料,总结一篇关于在自定义指 ...

  9. html页面选择图片上传时实现图片预览功能

    实现效果如下图所示 只需要将下面的html部分的代码放入你的代码即可 (注意引入jQuery文件和html头部的css样式,使用的是ajax提交) <!-- 需引入jQuery 引入样式文件 引 ...

  10. xml中运用js和jq

    1.点击事件参数为this 一般<a>标签中会使用href和onclick两种方式来进行进行页面跳转或执行动作,但是小编一般都会使用onclick来进行执行Ajax函数进行跳转,并同时使用 ...