Android Implementation Countdown

Source: Internet
Author: User

Objective

When opening the Welcome screen of the Iqiyi app, there is a countdown control in the top right corner. Enter the main screen after the countdown. Now let's implement this feature.

Method One

Using Java's class Timer,timertask and Android handler

Interface Welcome_activity.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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">    <TextViewAndroid:id= "@+id/count_down"Android:layout_width= "60DP"Android:layout_height= "60DP"Android:layout_alignparentright= "true"Android:layout_alignparenttop= "true"Android:layout_marginright= "16DP"Android:layout_margintop= "16DP"android:gravity= "Center"android:textsize= "32SP"Android:textcolor= "#50000000"Android:background= "@drawable/count_down_background"Tools:text= "2"        /></Relativelayout>
 PackageCom.example.counttimer;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.Window;ImportAndroid.widget.TextView; Public classWelcomeactivityextendsActivity {Private Final Static intCOUNT = 1; PrivateTextView Countdown; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_welcome);    Initview (); The first parameter of//sehedule is the time when the delay is started, and the third is how often it is executed. The units are all Ms.
So here is a message sent every second to the handler update UI.
Then three seconds later, in the second sehedule timer to jump to another interfacePrivate voidInitview () {Countdown=(TextView) Findviewbyid (R.id.count_down); FinalTimer timer =NewTimer (); Final LongEnd = System.currenttimemillis () + 1000*3; Timer.schedule (NewTimerTask () {@Override Public voidrun () {handler.sendemptymessage (COUNT); } }, 0, 1000); The second parameter meaning of the schedule here is that the method of running the run runs as soon as possible at this time Timer.schedule (NewTimerTask () {@Override Public voidrun () {Intent I=NewIntent (welcomeactivity. This, Secondactivity.class); I.addflags (Intent.flag_activity_clear_task); StartActivity (i); Finish (); Timer.cancel (); } }, NewDate (end)); } PrivateHandler Handler =NewHandler () {intnum = 2; Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseCOUNT:countDown.setText (string.valueof (num)); Num--; Break; default: Break; } }; }; }
Count_down_background.xml
<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android"Android:shape= "Rectangle">    <CornersAndroid:radius= "8DP"/>    <SolidAndroid:color= "#1e000000"/></Shape>

There's another empty activity that won't be posted.

The effect is as follows: Countdown to 0 Enter secondactivity

Method Two

Use the Android-encapsulated class Countdowntimer. In fact, the interior is also implemented with handler. The others are the same.

 PackageCom.example.counttimer;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.os.CountDownTimer;ImportAndroid.view.Window;ImportAndroid.widget.TextView; Public classWelcomeactivityextendsActivity {Private Final Static intCOUNT = 1; PrivateTextView Countdown; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_welcome);    Initview (); }    Private voidInitview () {Countdown=(TextView) Findviewbyid (R.id.count_down);
The two parameters of the Countdowntimer constructor are the first parameter representing the total time, and the second parameter represents the interval time.
It means that every XXX will callback the method Ontick, and then after XXX will callback OnFinish method. Countdowntimer Timer=NewCountdowntimer (3200,1000) { intnum = 2; @Override Public voidOnTick (Longmillisuntilfinished) {Countdown.settext (string.valueof (num)); Num--; } @Override Public voidOnFinish () {
Timing Completion Call Intent I=NewIntent (welcomeactivity. This, Secondactivity.class); I.addflags (Intent.flag_activity_clear_task); StartActivity (i); Finish (); } }; Timer.start (); } }

Android Implementation Countdown

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.