Implementation of countdownbuttonhelper

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

When we do the text message verification code function, in order to prevent users from endlessly obtaining the text message verification code, or misoperation, the verification code is messy, we generally add the countdown function on the button that gets the verification code, thus limiting the interval between users to get the verification code.

This article describes how to use countdowmtimer to implement the countdown function of the verification code.

First look


Countdowntimer is a class provided by the system for inverted counting. We can set the total countdown time and the countdown interval so that every fixed period of time passes, we can perform our operations in the callback function. The following are the callback methods and constructor of the countdowntimer class.

CountDownTimer timer = new CountDownTimer(AllTime,Intevel) {                 @Override         public void onTick(long millisUntilFinished) {                     }                 @Override         public void onFinish() {                   }      };

New countdowntimer (alltime, intevel)

The first parameter is the total time, and the second parameter is the countdown interval.

Ontick () is the callback function that countdown to the interval.

Onfinish () Is the function called after the timer ends.

The following shows my countdown help class.

package com.example.countdowntimerdemo;import android.os.CountDownTimer;import android.util.Log;import android.widget.Button;/** * 倒计时Button帮助类 *  * @author zhaokaiqiang * @see http://blog.csdn.net/zhaokaiqiang1992 */public class CountDownButtonHelper {// 倒计时timerprivate CountDownTimer countDownTimer;// 计时结束的回调接口private OnFinishListener listener;private Button button;/** *  * @param button *            需要显示倒计时的Button * @param defaultString *            默认显示的字符串 * @param max *            需要进行倒计时的最大值,单位是秒 * @param interval *            倒计时的间隔,单位是秒 */public CountDownButtonHelper(final Button button,final String defaultString, int max, int interval) {this.button = button;// 由于CountDownTimer并不是准确计时,在onTick方法调用的时候,time会有1-10ms左右的误差,这会导致最后一秒不会调用onTick()// 因此,设置间隔的时候,默认减去了10ms,从而减去误差。// 经过以上的微调,最后一秒的显示时间会由于10ms延迟的积累,导致显示时间比1s长max*10ms的时间,其他时间的显示正常,总时间正常countDownTimer = new CountDownTimer(max * 1000, interval * 1000 - 10) {@Overridepublic void onTick(long time) {// 第一次调用会有1-10ms的误差,因此需要+15ms,防止第一个数不显示,第二个数显示2sbutton.setText(defaultString + "(" + ((time + 15) / 1000)+ "秒)");Log.d("CountDownButtonHelper", "time = " + (time) + " text = "+ ((time + 15) / 1000));}@Overridepublic void onFinish() {button.setEnabled(true);button.setText(defaultString);if (listener != null) {listener.finish();}}};}/** * 开始倒计时 */public void start() {button.setEnabled(false);countDownTimer.start();}/** * 设置倒计时结束的监听器 *  * @param listener */public void setOnFinishListener(OnFinishListener listener) {this.listener = listener;}/** * 计时结束的回调接口 *  * @author zhaokaiqiang *  */public interface OnFinishListener {public void finish();}}
For more information about the test source code, see my GitHub.

Https://github.com/ZhaoKaiQiang/CountDownTimerDemo

Implementation of countdownbuttonhelper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.