Preface
In the open Archie Art and other app's welcome interface, the upper right corner has a countdown control. When the countdown is over, enter the main interface. Now we're going to implement this feature.
Method One:
Java-based class Timer,timertask and Android handler
Interface Welcome_activity.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout "xmlns:android=" /schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" match _parent "android:layout_height=" match_parent "> <textview android:id=" @+id/count_down "Android:layout_width" = "60DP" android:layout_height= "60DP" android:layout_alignparentright= "true" android:layout_alignparenttop= "true" a
ndroid: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"/> </Relat ivelayout>
Package Com.example.counttimer;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.Window;
Import Android.widget.TextView;
public class Welcomeactivity extends activity {private final static int COUNT = 1;
Private TextView Countdown;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_welcome);
Initview (); The first parameter of the//sehedule is the time when the delay is initiated for the initial start, and the third is how often it is executed at every interval. The units are all Ms. So here is a message every second to the handler update UI. And then three seconds later, it's time to jump to another interface in the second sehedule of the timer. private void Initview () {countdown = (TextView) Findviewbyid (r.id.count_
Down);
Final Timer timer = new timer ();
Final Long end = System.currenttimemillis () + 1000*3; Timer.schedule (New TimerTask () {@OveRride public void Run () {handler.sendemptymessage (COUNT);
}, 0, 1000); The second parameter meaning of the schedule here is the way to run the run inside as quickly as possible at this time Timer.schedule (new TimerTask () {@Override public void run () {Intent
i = new Intent (welcomeactivity.this, Secondactivity.class);
I.addflags (Intent.flag_activity_clear_task);
StartActivity (i);
Finish ();
Timer.cancel ();
}}, new Date (end);
Private Handler Handler = new Handler () {int num = 2; public void Handlemessage (android.os.Message msg) {switch (msg.what) {case COUNT:countDown.setText (String.valu
EOf (num));
num--;
Break
Default:break;
}
};
};
}
Count_down_background.xml
<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"http://schemas.android.com/apk/res/" Android "
android:shape=" Rectangle ">
<corners
android:radius=" 8DP "/>
<solid Android:color= "#1e000000"/>
</shape>
There's another empty activity that won't be posted.
The effect is as follows: Countdown to 0 into secondactivity
Method Two :
Use the Android encapsulated class Countdowntimer. In fact, the interior is also implemented with handler. Everything else is the same.
Package Com.example.counttimer;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.CountDownTimer;
Import Android.view.Window;
Import Android.widget.TextView;
public class Welcomeactivity extends activity {private final static int COUNT = 1;
Private TextView Countdown;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_welcome);
Initview (); private void Initview () {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.
The meaning is that every XXX will callback the method Ontick, then the XXX will callback the OnFinish method.
Countdowntimer timer = new Countdowntimer (3200,1000) {int num = 2;
@Override public void Ontick (long millisuntilfinished) {countdown.settext (string.valueof (num));
num--; @Override public void OnFinish () {//Timing completion calls Intent i = new Intent (WelComeactivity.this, Secondactivity.class);
I.addflags (Intent.flag_activity_clear_task);
StartActivity (i);
Finish ();
}
};
Timer.start ();
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.