Android Custom Countdown Control

Source: Internet
Author: User
Tags time in milliseconds

Order:

Recently more and more apps are registered with mobile phone number, one for the convenience of user memory, the second is to increase the security of user accounts. We need to enter the SMS verification code when we are doing the trading operation or changing the password. This control will need to have a countdown function, here is the main summary of several common implementation methods.

The method of realizing the countdown in 1.Android the first kind: directly using handler's message mechanism to implement

This feeling is the most primitive, not much to say here.

The second type: Timer and TimerTask

Basic use: Get Timer and TimerTask object, then start, Countdown logic written in Handler

Private timer mtimer=new timer ();

Private TimerTask mtimertask= New TimerTask () {

@Override

public void Run () {

Mhandler.sendemptymessage (Counttime);

}

};

Countdown task Start

Mtimer.schedule (mtimertask, 0, Monetime);

Be aware of the release of resources here: emptying resources where appropriate

/*** Clear Time */

private void Cleartimer () {

if (mtimertask! = null) {

Mtimertask.cancel ();

Mtimertask = null;

}

if (Mtimer! = null) {

Mtimer.cancel ();

Mtimer = null;

}

}

Third: Use the native Countdown class directly from Android (also using handler)

Btngetverificationcode.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

This is the native Countdown class, the first parameter is the total number of milliseconds, the second is the number of milliseconds to Countdown.

New Countdowntimer (10000, 1000) {

@Override

public void OnTick (long millisuntilfinished) {

The method in the countdown

Btngetverificationcode.setenabled (FALSE);

Btngetverificationcode.settext (millisuntilfinished/1000 + "SEC");

}

@Override

public void OnFinish () {

The method after the countdown is over

Btngetverificationcode.setenabled (TRUE);

Btngetverificationcode.settext ("Duplicate Access Verification Code");

}

}.start ();//How to start the countdown

}

});

This approach seems to be the simplest, but there are several obvious flaws:

1. The number of seconds displayed at the beginning of a countdown may not be accurate

2. The countdown process may be lost in one second

3. Countdown to the last display "1 seconds", then will be 1 seconds, not expected 0 seconds. The lag is only displayed after 1 seconds.

2. Mall Countdown Library

Here is a countdown library on GitHub: Commodity Countdown Library

3. Custom Countdown Control

To simplify the use of the countdown control, reference the commodity Countdown Library and write a custom control. The effect is as follows


The specific implementation principle is: Inherit TextView, re-Ontouch () method, use timer to Countdown.

4. Custom Countdown Control basic Use the first step: Add the control to the layout:

<android:id= "@+id/msgctv"

Android:layout_width= "Wrap_content"

Android:layout_gravity= "Center_horizontal"

android:layout_height= "Wrap_content"

android:padding= "16DP"/>


Step two: Join in the right place

Msgcounttimeview.isallowrun (TRUE);//Allow starting Countdown

Description of some APIs

counttimeview.settotaltime (10000);//Set total time in milliseconds

setinittext (" ")//Set the initialized text

setprefixruntext (" Code remaining Time ")//Set the text prefix of the runtime

setsuffixruntext (" ")//Set the suffix of the text at run time

setfinishtext (" code regain ")//text after set end

Settimecolor (color.bluelight);//sets the color of the run-time number of seconds

5. Source Address

Custom Countdown Control

Android Custom Countdown Control

Related Article

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.