Android使用CountDownTimer模拟短信验证倒计时
本文为大家分享了CountDownTimer模拟短信验证倒计时的具体代码,供大家参考,具体内容如下
内容:介绍倒计时CountDownTimer的基本使用方法。模拟短信验证
步骤:
1、继承CountDownTimer,重写onTick()、onFinish()
2、代码中new出CountDownTimer子类,传好参数,调用start()执行
代码如下:
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.leixiansheng.countdowntimer.MainActivity"> <TextView android:id="@+id/tv_getMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="获取短信验证码" android:background="@color/colorPrimaryDark" android:textSize="16sp" android:textColor="#ffffffff" /> </RelativeLayout>
TimerCount
package com.example.leixiansheng.countdowntimer; import android.os.CountDownTimer; import android.widget.TextView; /** * Created by Leixiansheng on 2018/7/18. */ public class TimerCount extends CountDownTimer { private TextView mTextView; public TimerCount(long millisInFuture, long countDownInterval, TextView textView) { super(millisInFuture, countDownInterval); mTextView = textView; } @Override public void onTick(long millisUntilFinished) { mTextView.setClickable(false); mTextView.setText("重新获取" + millisUntilFinished / 1000 + "秒"); } @Override public void onFinish() { mTextView.setClickable(true); mTextView.setText("获取短信验证码"); } }
Main
package com.example.leixiansheng.countdowntimer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textView = (TextView) findViewById(R.id.tv_getMsg); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * millisInFuture:要计数的总时长 * countDownInterval:每隔多少秒响应 */ TimerCount timerCount = new TimerCount(5000, 1000, textView); timerCount.start(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android性能优化之ViewPagers + Fragment缓存优化
这篇文章主要介绍了Android性能优化之ViewPagers+Fragment缓存优化,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-08-08Android动画之逐帧动画(Frame Animation)基础学习
大家都知道逐帧动画是一种常见的动画形式,其原理是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。下面我们就来学习下Android中逐帧动画的基础知识,有需要的可以参考借鉴。2016-09-09android播放视频时在立体声与单声道之间切换无变化原因分析及解决
使用第三方视频播放器,有立体声与单声道之间切换,发现切换后无作用,原因是由于在HAL层默认没有处理上层发的stereo 转mono的命令,具体的解决方法如下2013-06-06解析:继承ViewGroup后的子类如何重写onMeasure方法
本篇文章是对继承ViewGroup后的子类如何重写onMeasure方法进行了详细的分析介绍,需要的朋友参考下2013-06-06
最新评论