Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Android 获取验证码倒计时实现

Android 获取验证码倒计时实现

作者头像
网罗开发
发布于 2021-01-29 06:52:52
发布于 2021-01-29 06:52:52
87600
代码可运行
举报
文章被收录于专栏:网罗开发网罗开发
运行总次数:0
代码可运行
1. 验证码输入框和获取验证码按钮布局

xml代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        <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代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
* 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. 倒计时函数
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

   //倒计时函数
   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. 清除倒计时函数,解决验证码输入正确后停止计时
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
private void clearTimer() {
       if (task != null) {
           task.cancel();
           task = null;
       }
       if (timer != null) {
           timer.cancel();
           timer = null;
       }
   }

希望可以帮助大家 如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-10-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 网罗开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Android发送验证码倒计时,时间倒计时
Demo地址:https://github.com/yechaoa/TimeDemo
yechaoa
2022/06/10
7750
Android发送验证码倒计时,时间倒计时
Android 实现倒计时的简单方式
一、布局实现(使用 FrameLayout 悬浮在广告的右上角,显示倒计时的 TextView 的宽高尽量不要写死,要考虑字体很多的情况!!) <FrameLayout android:id="@+id/start_skip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight=
程思扬
2022/01/10
6720
countdown timer plus_android studio计时器
In this android countdown timer example, we’ll implement a timer object to display the progress in a ProgressBar. The application we’ll build in this tutorial is a useful component in Quiz apps where the time left to complete the level is displayed graphically to enhance the user experience.
全栈程序员站长
2022/11/04
1K0
Android 短信验证码登录注册
我这里主要讲解的是使用xutils进行开发的,所以在开发之前一定要先导入xutils项目包。
全栈程序员站长
2022/09/16
8.4K0
倒计时工具类
在APP开发过程中我们通常会有一个获取手机验证码这样的一个操作,其中有一个倒计时 一般来说就是60s,倒计时期间不可点击 ,计时结束之后重新获取验证码。如下图所示
晨曦_LLW
2020/09/25
6350
AndroidStudio项目制作倒计时模块
大家好,我是 Vic,今天给大家带来AndroidStudio项目制作倒计时模块的概述,希望你们喜欢
达达前端
2019/07/03
7000
android 特卖列表倒计时卡顿问题
在Android的开发中,我们经常遇见倒计时的操作,通常使用Timer和Handler共同操作来完成。当然也可以使用Android系统控件CountDownTimer,这里我们封装成一个控件,也方便大
xiangzhihong
2018/02/05
1.1K0
android 特卖列表倒计时卡顿问题
相关推荐
Android发送验证码倒计时,时间倒计时
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验