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. 关于加载font-awesome文字显示不出来

    关于font-awesome的网站 中文网:http://www.fontawesome.com.cn/get-started/ 官网:https://fontawesome.com/ 中文网4.7: ...

  2. MySQL中的自适应哈希索引

    众所周知,InnoDB使用的索引结构是B+树,但其实它还支持另一种索引:自适应哈希索引. 哈希表是数组+链表的形式.通过哈希函数计算每个节点数据中键所对应的哈希桶位置,如果出现哈希冲突,就使用拉链法来 ...

  3. shell编写小技巧整理

    1. if和else语句可以进行嵌套.if的条件判断部分可能会变得很长,可以使用逻辑运算符将它变得简洁一些. [ condition ] && action :如果condition为 ...

  4. SignalR使用笔记

    最近项目要求添加一个给用户发送消息的功能,就决定使用SignalR.翻到了以前学习SignalR的学习笔记,基本是官方文档的简版整理,便于快速阅览和实现. 1. nuget添加signalr引用: a ...

  5. 干货|一文读懂 Spring Data Jpa!

    有很多读者留言希望松哥能好好聊聊 Spring Data Jpa!其实这个话题松哥以前零零散散的介绍过,在我的书里也有介绍过,但是在公众号中还没和大伙聊过,因此本文就和大家来仔细聊聊 Spring D ...

  6. 《HelloGitHub》第 31 期

    公告 网站新增了 Web 服务器排行榜.数据库排行榜 <HelloGitHub>第 31 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣 ...

  7. HandlerInterceptor里@Autowired对象为空的解决方法

    That's because Spring isn't managing your PagePopulationInterceptor instance. You are creating it yo ...

  8. Linux 软硬链接的区别及目录权限对软硬链接的影响

    目录权限的影响 首先,构建实验环境如下图: 有 A.B 两个目录,A 目录下建有 test 文件,且文件中的内容为 "Hello".B 目录下分别创建 test 文件的硬链接 te ...

  9. 如何發佈一個完整Node.js Module

    本文會透過以下幾個段落,讓各位一步一步學習如何寫一個自已的Node.js Module並且發佈到npm package上 Node.js Module 結構 我們先建立一個 NodeModuleDem ...

  10. c#上位机与三菱PLC(FX3U)串口通讯

    项目中会经常用到上位机与PLC之间的串口通信,本文介绍一下C#如何编写上位机代码 与三菱FX3U进行通讯 1. 第一种方法是自己写代码实现,主要代码如下: //对PLC的Y7进行置1 byte[] Y ...