Android使用CountDownTimer模拟短信验证倒计时
更新时间:2018年07月20日 09:20:31 作者:Mr_Leixiansheng
这篇文章主要为大家详细介绍了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中BroadcastReceiver的两种注册方式(静态和动态)详解
这篇文章主要介绍了深入Android中BroadcastReceiver的两种注册方式(静态和动态)详解,具有一定的参考价值,有需要的可以了解一下。2016-12-12
Android中控件GridView实现设置行列分割线的方法示例
这篇文章主要介绍了利用Android中控件GridView实现设置行列分割线的方法,文中给出了详细的介绍与示例代码,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。2017-01-01
Android中使用socket使底层和framework通信的实现方法
native和framework的通信是通过jni,但是这一般只是framework调用native,native如果有消息要怎样通知上层 呢?android中GSP模块提供一种解决思路,但是实现有些复杂,这里介绍一种使用socket通信的方法可以使native和framework自由通信,感兴趣的朋友一起看看吧2016-11-11


最新评论