Android 获取验证码倒计时实现
Android 获取验证码倒计时实现
1. 验证码输入框和获取验证码按钮布局
xml代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >
<EditText
android:id="@+id/phonetext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:inputType="number"
android:hint="请输入短信验证码"
android:background="@null"/>
<Button
android:id="@+id/timebutton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textSize="16dp"
android:background="@drawable/tv_timemessage_bg"
android:text="获取"
/>
</LinearLayout>
效果如下:

2. 根据id设置Button点击事件触发倒计时
JAVA代码:
/**
* Created by fby on 2017/9/11.
*/
public class ChargepsdActivity extends Activity {
private Button timeButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chargepsd);
timeButton = (Button) findViewById(R.id.timebutton);
//new倒计时对象,总共的时间,每隔多少秒更新一次时间
final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);
//设置Button点击事件触发倒计时
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myCountDownTimer.start();
}
});
}
3. 倒计时函数
//倒计时函数
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//计时过程
@Override
public void onTick(long l) {
//防止计时过程中重复点击
timeButton.setClickable(false);
timeButton.setText(l/1000+"秒");
}
//计时完毕的方法
@Override
public void onFinish() {
//重新给Button设置文字
timeButton.setText("重新获取");
//设置可点击
timeButton.setClickable(true);
}
}
}
4. 清除倒计时函数,解决验证码输入正确后停止计时
private void clearTimer() {
if (task != null) {
task.cancel();
task = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
Android 获取验证码倒计时实现的更多相关文章
- Andorid实现点击获取验证码倒计时效果
这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文 我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取 ...
- angular中service封装$http做权限时拦截403等状态及获取验证码倒计时、跨域问题解决
封装$http.做权限时拦截403等状态及获取验证码倒计时: 拦截接口返回状态 var app = angular.module('app'); app.factory('UserIntercepto ...
- Android中注册获取验证码倒计时按钮
public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...
- 前端学习——ionic/AngularJs——获取验证码倒计时按钮
按钮功能为:点击"获取验证码"--按钮不可用-设置倒计时-60秒后重新获取. 代码借鉴于:http://plnkr.co/edit/Swj82MpJSix3a47jZRHP?p= ...
- iOS开发之--获取验证码倒计时及闪烁问题解决方案
大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t q ...
- js点击按钮获取验证码倒计时
//发送验证码倒计时 var clock = ''; var nums = 60; var btn; $("#btnGetVerCode").click(function () { ...
- Vue 获取验证码倒计时组件
子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...
- iOS项目中获取验证码倒计时及闪烁问题解决方案
-(void)startTime{ __block int timeout= 59; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queu ...
- JQuery 获取验证码倒计时
HTML代码: <button id="btn">点击获取验证码</button> Jquery:代码: $(document).ready(functio ...
随机推荐
- Vue2.x源码学习笔记-Vue源码调试
如果我们不用单文件组件开发,一般直接<script src="dist/vue.js">引入开发版vue.js这种情况下debug也是很方便的,只不过vue.js文件代 ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- appium-desktop定位元素原理
初衷 最近在编写Android App自动化用例,其中元素定位相对来说耗费的时间比较长.我们都知道Appium-desktop拥有自己的录制功能,我们就在想是不是可以把录制功能跟我司的自动化框架(AT ...
- springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...
- Python基础(生成器)
二.生成器(可以看做是一种数据类型) 描述: 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我 ...
- Android项目目录结构模板以及简单说明【简单版】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 开发Android项目的时候,一般都是一边开发一边根据需求创建目录(包.module),那么我呢就根据以往的项目经验,整理出一个比较 ...
- 基于mapreduce实现图的三角形计数
源代码放在我的github上,想细致了解的可以访问:TriangleCount on github 一.实验要求 1.1 实验背景 图的三角形计数问题是一个基本的图计算问题,是很多复杂 ...
- (转)使用JMeter进行Web压力测试
使用JMeter进行压力测试 说到压力测试,一般第一反应都是LoadRunner.这个软件也确实是自动化测试的一个事实标准.无奈这个软件太过庞大,以及不能在MacOS上使用.我由于项目的需要,需要对一 ...
- ASP.NET Core的JWT的实现(自定义策略形式验证).md
既然选择了远方,便只顾风雨兼程 __ HANS许 在上篇文章,我们讲了JWT在ASP.NET Core的实现,基于中间件来实现.这种方式有个缺点,就是所有的URL,要嘛需要验证,要嘛不需要验证,没有办 ...
- 39.QT-Qtxlsx库使用
之前参考博客https://blog.csdn.net/c3060911030/article/details/51560239下载Qtxlsx库,然后编译的时候,显示: error: invalid ...