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 }
本文来自博客园,作者:西瓜皮不甜,转载请注明原文链接:https://www.cnblogs.com/Jieth/p/5160463.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!