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 @   西瓜皮不甜  阅读(366)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示