日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android使用CountDownTimer倒计时

發布時間:2024/8/26 Android 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Android使用CountDownTimer倒计时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。