Android Combat Simple Tutorial-41st gun (show Countdown button-for obtaining verification code)

Source: Internet
Author: User

Recently doing the ability to get verification code. Taking into account the excellent user experience, decided to make a countdown button buttons, on the Internet to check some information, very simple can be achieved. I have written a small demo that everyone can apply to their projects.

One, the code

1.activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <button        android:id= "@+id/btn_time"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:text= "Get Verification Code"/></relativelayout>

2.mainactivity.java:

Package Com.example.timebutton;import Android.app.activity;import Android.os.bundle;import Android.os.countdowntimer;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;public class Mainactivity extends Activity {private Button Mtimebutton;private timecount time; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Mtimebutton = (Button) Findviewbyid (r.id.btn_time); time = new Timecount (60000, n); Mtimebutton.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {Time.start ();}});}            /** * Inherit Countdown class * * @author TML * */class Timecount extends Countdowntimer {/** * Construction method * * @param millisinfuture *  Total Countdown long milliseconds * @param countdowninterval * countdown interval */public timecount (long millisinfuture, long Countdowninterval) {Super (millisinfuture, countdowninterval);} @Overridepublic void OnTick (Long millisuntilfinished) {mtimebutton.setenabled (false); Mtimebutton.settext ( millisuntilfinished/1000 + "Seconds");} @Overridepublic void OnFinish () {//timed end mtimebutton.setenabled (true); Mtimebutton.settext ("Get" Again);}}}

Second, the running example:





Summary: How to use the Countdowntimer class:

Structure

Inheritance relationship

Public abstract class Countdowntimer extends Object

Java.lang.Object

Android.os.CountDownTimer

class overview

A countdown that stops after a certain period of time and is notified at regular intervals during the countdown (translator: Triggering the OnTick method), the following sample shows a 30s countdown in a text box :

NewCountdowntimer(30000, +){

      Public voidOnTick(Longmillisuntilfinished){

Mtextfield.SetText("seconds remaining:"+millisuntilfinished/ +);

     }

      public  void  onfinish ()  {

         mtextfield setText ( Span style= "line-height:1.5; font-size:10pt; Font-family: "courier new"; Color:rgb (0, 136, 0) ">" done! " );

     }

}. Start ();

The OnTick call is synchronous, ensuring that the call does not occur until the previous call is complete.

The synchronization mechanism here is mainly used to: the implementation of OnTick need to run a lot more time than the countdown interval more important things.

constructor function

public Countdowntimer (Long millisinfuture, long Countdowninterval)

Number of references

                    millisinfuture    start () onfinish ()

Countdowninterval Receive OnTick (Long) the time interval for the callback.

(Translator Note: unit milliseconds)

Public methods

Public final void Cancel ()

Cancel Countdown (translator: After the cancellation, the restart will start again Countdown)

public abstract void onfinish ()

called when the countdown is complete

public abstract void OnTick (Long millisuntilfinished)

fixed interval is called

Number of references

millisuntilfinished countdown time remaining.

Public synchronized final Countdowntimer start ()

Start Countdown



Thank you, my favorite friends pay attention to me!

Android Combat Simple Tutorial-41st gun (show Countdown button-for obtaining verification code)

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.