Android使用CountDownTimer倒计时

1、布局文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical" >
 5     <TextView
 6         android:id="@+id/textView1"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="TextView" />
10 </LinearLayout>

2、调用

 1 package com.best.daojishi;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 
 7 public class MainActivity extends Activity {
 8     CountdownUtil c;
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         TextView textView = (TextView) findViewById(R.id.textView1);
14 
15         c = new CountdownUtil(60000000, textView);
16         c.countdown();
17     }
18     @Override
19     protected void onDestroy() {
20         // TODO Auto-generated method stub
21         super.onDestroy();
22         c.stopThread();
23     }
24 }

3、倒计时

 1 package com.best.daojishi;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.TimeZone;
 5 import android.os.CountDownTimer;
 6 import android.widget.TextView;
 7 /**
 8  * 倒计时
 9  * */
10 public class CountdownUtil {
11     private long time;
12     TextView counetdownView;
13     CountdownThread thread;
14     SimpleDateFormat formatter;
15     String hms;
16     /**
17      * @time:时间差(指定的一段时间长),时间戳
18      * @counetdownView:TextView显示倒计时
19      * */
20     public CountdownUtil(long time, TextView counetdownView) {
21         this.time = time;
22         this.counetdownView = counetdownView;
23     }
24     /**
25      * 倒计时
26      * */
27     public void countdown(){
28         formatter = new SimpleDateFormat("HH:mm:ss");// 初始化Formatter的转换格式。
29         formatter.setTimeZone(TimeZone.getTimeZone("GMT +8:00"));//设置时区(北京),如果你不设置这个,你会发现你的时间总会多出来8个小时
30 
31         thread = new CountdownThread(time, 1000);// 构造CountDownTimer对象
32         thread.start();
33     }
34     class CountdownThread extends CountDownTimer {
35         public CountdownThread(long millisInFuture, long countDownInterval) {
36             super(millisInFuture, countDownInterval);
37             // TODO Auto-generated constructor stub
38         }
39         @Override
40         public void onTick(long millisUntilFinished) {
41             hms = formatter.format(millisUntilFinished);//转化成  "00:00:00"的格式
42             counetdownView.setText(hms);
43         }
44 
45         @Override
46         public void onFinish() {
47             // TODO Auto-generated method stub
48             //倒计时结束时触发
49             counetdownView.setText("00:00:00");
50         }
51     }
52     /**
53      * 终止线程
54      * */
55     public void stopThread(){
56         thread.cancel();
57     }
58 }

 

posted @ 2016-01-26 15:08  西瓜皮不甜  阅读(366)  评论(0编辑  收藏  举报