Android 获取验证码倒计时实现

2017年10月24日 09:55:41 FBY展菲 阅读数:2002
 
 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36478920/article/details/78326305

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 获取验证码倒计时实现的更多相关文章

  1. Andorid实现点击获取验证码倒计时效果

    这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文   我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取 ...

  2. angular中service封装$http做权限时拦截403等状态及获取验证码倒计时、跨域问题解决

    封装$http.做权限时拦截403等状态及获取验证码倒计时: 拦截接口返回状态 var app = angular.module('app'); app.factory('UserIntercepto ...

  3. Android中注册获取验证码倒计时按钮

    public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...

  4. 前端学习——ionic/AngularJs——获取验证码倒计时按钮

     按钮功能为:点击"获取验证码"--按钮不可用-设置倒计时-60秒后重新获取. 代码借鉴于:http://plnkr.co/edit/Swj82MpJSix3a47jZRHP?p= ...

  5. iOS开发之--获取验证码倒计时及闪烁问题解决方案

    大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t q ...

  6. js点击按钮获取验证码倒计时

    //发送验证码倒计时 var clock = ''; var nums = 60; var btn; $("#btnGetVerCode").click(function () { ...

  7. Vue 获取验证码倒计时组件

    子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...

  8. iOS项目中获取验证码倒计时及闪烁问题解决方案

    -(void)startTime{ __block int timeout= 59; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queu ...

  9. JQuery 获取验证码倒计时

    HTML代码: <button id="btn">点击获取验证码</button> Jquery:代码: $(document).ready(functio ...

随机推荐

  1. Windows Server 2016-Netdom Join之客户端加域(二)

    上章节我们介绍了如何通过图形化界面实现用户加域操作,本章节为大家简单介绍如何通过netdom join命令实现计算机加域操作.具体信息如下: 将工作站或成员服务器加入域.将计算机加入域的行为会为域中的 ...

  2. WPF 通过透明度遮罩和变换制作倒影效果

      倒影效果 代码 <Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http:/ ...

  3. PS抠图简单粗暴2种方法

    1.矩形框住要抠的→ Ctrl+j →选中图层 →矩形框住 →图像裁剪另存为 2.钢笔工具框住要抠的→ Ctrl+Enter→ Ctrl+j→.钢笔框住→图像裁剪另存为

  4. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...

  5. 《代码整洁之道》(Clean Code)- 读书笔记

    一.关于Bob大叔的Clean Code <代码整洁之道>主要讲述了一系列行之有效的整洁代码操作实践.软件质量,不但依赖于架构及项目管理,而且与代码质量紧密相关.这一点,无论是敏捷开发流派 ...

  6. 在vue项目中添加全局提示框

    1.写一个提示框组件:msg.vue (注明:这里用两个div的原因是成功和失败的提示不能用同一个div,因为他们可能紧接着出现,所以不能从始至终只有一个提示框在工作) 2.写一个调用此组建的js : ...

  7. 💈 线程间互访助手类 (EN)

    Conmajia © 2012, 2018 Published on August 5th, 2012 Updated on February 2nd, 2019 Introduction While ...

  8. EF Core 快速上手——EF Core的三种主要关系类型

    系列文章 EF Core 快速上手--EF Core 入门 本节导航 三种数据库关系类型建模 Migration方式创建和习修改数据库 定义和创建应用DbContext 将复杂查询拆分为子查询   本 ...

  9. ASP.NET Core 基于JWT的认证(一)

    ASP.NET Core 基于JWT的认证(一) Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计 ...

  10. SQLServer存储过程编写规则

    SQLServer编写规则 1.  存储过程 a)         在程序应用中,对于数据库“写”操作的功能通过存储过程来实现. b)        存储过程命名: SP_+表名(+功能名) 对于一个 ...