Joke Collection Website - Public benefit messages - How to count down Android?
How to count down Android?
Use java class timers, timer tasks and android handlers.
Interface welcome_activity.xml
& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt
& ltrelative layout xmlns:Android = "/apk/RES/Android "
xmlns:tools="/tools "
Android:layout _ width = " match _ parent "
Android:layout _ height = " match _ parent " & gt;
& lt text view
android:id="@+id/count_down "
android:layout_width="60dp "
android:layout_height="60dp "
Android:layout _ alignParentRight = " true "
Android:layout _ alignParentTop = " true "
Android:layout _ margin right = " 16dp "
Android:layout _ margin top = " 16dp "
Android: Gravity = "Center"
android:textSize="32sp "
android:textColor="#50000000 "
Android:background = " @ drawable/count _ down _ background "
Tool: text="2 "
/& gt;
& lt/relative layout & gt;
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;
Open class WelcomeActivity expansion activities {
Private final static int count =1;
Private TextView countdown;
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
RequestWindowFeature (window. Feature _ Number _ Title);
setContentView(r . layout . activity _ welcome);
init view();
}
//The first parameter of the hedule is the time of the first startup delay, and the third parameter is the frequency of execution. The unit is ms .// So here is to send a message to the handler and update the UI every second. //After three seconds, in the second schedule of the timer, it is time to jump to another interface.
Private void initView() {
count down =(TextView)findViewById(r . id . count _ down);
Final timer = new timer ();
final long end = system . current time millis()+ 1000 * 3;
Timer.schedule (new TimerTask() {
@ Overlay
Public invalid operation () {
Handler.sendEmptyMessage (count);
}
}, 0, 1000);
//The second parameter of schedule here means to run the method in run as soon as possible at this time.
Timer.schedule (new TimerTask() {
@ Overlay
Public invalid operation () {
Intent I = new Intent(welcome activity . this,second activity . class);
i.addFlags(Intent。 Flag _ Activity _ Clear _ Task);
Start the activity (1);
finish();
timer . cancel();
}
}, new date (end));
}
Private handler handler = new Handler(){
int num = 2;
public void handle message(Android . OS . message msg){
switch (msg.what) {
Case count:
count down . settext(string . value of(num));
num-;
Break;
Default value:
Break;
}
};
};
}
count_down_background.xml
& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt
& ltshape xmlns:Android = "/apk/RES/Android "
Android:shape = " rectangle " & gt;
& lt dilemma
Android:radius = " 8dp "/& gt;
& lt solid
Android:color = " # 1e 000000 "/>;
& lt/shape & gt;
Method 2:
Class CountDownTimer encapsulated by android. 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;
Open class WelcomeActivity expansion activities {
Private final static int count =1;
Private TextView countdown;
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
RequestWindowFeature (window. Feature _ Number _ Title);
setContentView(r . layout . activity _ welcome);
init view();
}
Private void initView() {
count down =(TextView)findViewById(r . id . count _ down); //The two parameters of the //CountDownTimer constructor are: the first parameter represents the total time and the second parameter represents the interval time. //means that the onTick method will be called back every xxx, and then the onFinish method will be called back after xxx.
Countdowntimer = new CountDownTimer(3200, 1000) {
int num = 2;
@ Overlay
Public void on tick (long millis until finished) {
count down . settext(string . value of(num));
num-;
}
@ Overlay
Public void onFinish() {// completes the call regularly.
Intent I = new Intent(welcome activity . this,second activity . class);
i.addFlags(Intent。 Flag _ Activity _ Clear _ Task);
Start the activity (1);
finish();
}
};
timer . start();
}
}
- Related articles
- What should I do if I forget my password on Baotou Education Big Data Platform?
- How to set Xiaomi 1 SMS wallpaper?
- How to check the record of QQ voice on mobile phone?
- There is no SMS prompt for Chengdu's house purchase review.
- Propaganda slogan of preventing new network telecom fraud
- SMS recovery tool Android
- Provisions on the return of Hubei and Han people to Tangshan to move to an 'an.
- How to delete the transfer record of China Construction Bank's mobile banking?
- What do you mean by an ex-girlfriend's text message?
- Will the name of the cardholder be displayed after the Pos machine swipes the card?