Android實(shí)現(xiàn)倒計(jì)時(shí)效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)倒計(jì)時(shí)效果的具體代碼,供大家參考,具體內(nèi)容如下
一個(gè)倒計(jì)時(shí)的效果
先看效果圖:
直接上代碼:
這里是關(guān)于倒計(jì)時(shí) …天時(shí)分秒…的邏輯判斷
/** * 倒計(jì)時(shí)計(jì)算 */ private void computeTime() { mSecond--; if (mSecond < 0) { mMin--; mSecond = 59; if (mMin < 0) { mMin = 59; mHour--; if (mHour < 0) { // 倒計(jì)時(shí)結(jié)束 mHour = 23; mDay--; if(mDay < 0){ // 倒計(jì)時(shí)結(jié)束 mDay = 0; mHour= 0; mMin = 0; mSecond = 0; } } } } }
定時(shí)器主要代碼如下…當(dāng)然也可以開線程或者開后臺(tái)服務(wù)來處理…只是沒那種必要…定時(shí)器就可以搞定容易控制…畢竟倒計(jì)時(shí)時(shí)間起點(diǎn)…你總得后臺(tái)獲取吧,不是做時(shí)鐘鬧鐘…如果是做時(shí)鐘鬧鐘…拿你也不用考慮后臺(tái)服務(wù)或者自己開線程…而是使用AlarmManager來實(shí)現(xiàn)
/** * 開啟倒計(jì)時(shí) * //time為Date類型:在指定時(shí)間執(zhí)行一次。 * timer.schedule(task, time); * //firstTime為Date類型,period為long,表示從firstTime時(shí)刻開始,每隔period毫秒執(zhí)行一次。 * timer.schedule(task, firstTime,period); * //delay 為long類型:從現(xiàn)在起過delay毫秒執(zhí)行一次。 * timer.schedule(task, delay); * //delay為long,period為long:從現(xiàn)在起過delay毫秒以后,每隔period毫秒執(zhí)行一次。 * timer.schedule(task, delay,period); */ private void startRun() { TimerTask mTimerTask = new TimerTask() { @Override public void run() { Message message = Message.obtain(); message.what = 1; timeHandler.sendMessage(message); } }; mTimer.schedule(mTimerTask,0,1000); }
修改界面,利用handler來提醒更新界面
private Handler timeHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { computeTime(); mDays_Tv.setText(mDay+"");//天數(shù)不用補(bǔ)位 mHours_Tv.setText(getTv(mHour)); mMinutes_Tv.setText(getTv(mMin)); mSeconds_Tv.setText(getTv(mSecond)); if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) { mTimer.cancel(); } } } }; private String getTv(long l){ if(l>=10){ return l+""; }else{ return "0"+l;//小于10,,前面補(bǔ)位一個(gè)"0" } }
附帶主activity的代碼…
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private RelativeLayout countDown; // 倒計(jì)時(shí) private TextView mDays_Tv, mHours_Tv, mMinutes_Tv, mSeconds_Tv; private long mDay = 23;// 天 private long mHour = 11;//小時(shí), private long mMin = 56;//分鐘, private long mSecond = 32;//秒 private Timer mTimer; private Handler timeHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { computeTime(); mDays_Tv.setText(mDay+"");//天數(shù)不用補(bǔ)位 mHours_Tv.setText(getTv(mHour)); mMinutes_Tv.setText(getTv(mMin)); mSeconds_Tv.setText(getTv(mSecond)); if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) { mTimer.cancel(); } } } }; private String getTv(long l){ if(l>=10){ return l+""; }else{ return "0"+l;//小于10,,前面補(bǔ)位一個(gè)"0" } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTimer = new Timer(); countDown = (RelativeLayout) findViewById(R.id.countdown_layout); mDays_Tv = (TextView) findViewById(R.id.days_tv); mHours_Tv = (TextView) findViewById(R.id.hours_tv); mMinutes_Tv = (TextView) findViewById(R.id.minutes_tv); mSeconds_Tv = (TextView) findViewById(R.id.seconds_tv); startRun(); } /** * 開啟倒計(jì)時(shí) * //time為Date類型:在指定時(shí)間執(zhí)行一次。 * timer.schedule(task, time); * //firstTime為Date類型,period為long,表示從firstTime時(shí)刻開始,每隔period毫秒執(zhí)行一次。 * timer.schedule(task, firstTime,period); * //delay 為long類型:從現(xiàn)在起過delay毫秒執(zhí)行一次。 * timer.schedule(task, delay); * //delay為long,period為long:從現(xiàn)在起過delay毫秒以后,每隔period毫秒執(zhí)行一次。 * timer.schedule(task, delay,period); */ private void startRun() { TimerTask mTimerTask = new TimerTask() { @Override public void run() { Message message = Message.obtain(); message.what = 1; timeHandler.sendMessage(message); } }; mTimer.schedule(mTimerTask,0,1000); } /** * 倒計(jì)時(shí)計(jì)算 */ private void computeTime() { mSecond--; if (mSecond < 0) { mMin--; mSecond = 59; if (mMin < 0) { mMin = 59; mHour--; if (mHour < 0) { // 倒計(jì)時(shí)結(jié)束 mHour = 23; mDay--; if(mDay < 0){ // 倒計(jì)時(shí)結(jié)束 mDay = 0; mHour= 0; mMin = 0; mSecond = 0; } } } } } }
附帶xml的代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/countdown_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" > <RelativeLayout android:id="@+id/daojishi_rl" android:layout_width="match_parent" android:layout_height="40.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:gravity="center" > <ImageView android:id="@+id/describe_iv" android:layout_width="40dp" android:layout_height="40dp" android:src="@mipmap/img" android:scaleType="fitXY" android:gravity="center_vertical" /> <TextView android:id="@+id/describe_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="5.0dip" android:layout_toRightOf="@+id/describe_iv" android:text="距離開團(tuán)還有" android:textSize="25sp" /> <TextView android:id="@+id/days_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:padding="4dp" android:layout_toRightOf="@+id/describe_tv" android:background="#c2c2c2" android:gravity="center" android:text="" android:textSize="20sp" /> <TextView android:id="@+id/colon0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5.0dip" android:layout_marginRight="3.0dip" android:layout_toRightOf="@+id/days_tv" android:text="天" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/daojishi_rl" android:gravity="center_horizontal" > <TextView android:id="@+id/hours_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/colon1" android:background="#c2c2c2" android:gravity="center" android:text="23" android:padding="3dp" android:textSize="20sp" /> <TextView android:id="@+id/colon1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" android:layout_toLeftOf="@+id/minutes_tv" android:text=":" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/minutes_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/colon2" android:background="#c2c2c2" android:gravity="center" android:text="59" android:padding="3dp" android:textSize="20sp" /> <TextView android:id="@+id/colon2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" android:layout_toLeftOf="@+id/seconds_tv" android:text=":" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/seconds_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="#c2c2c2" android:gravity="center" android:text="59" android:padding="3dp" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
完美實(shí)現(xiàn),直接用就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)拍照、錄像、錄音代碼范例
這篇文章主要介紹了Android實(shí)現(xiàn)拍照、錄像、錄音代碼的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字
本文主要介紹了Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04java,Android:在eclipse中的快捷鍵(經(jīng)典收藏)
下面的快捷鍵是常用的,本人就本身喜好且常用的收拾一下,現(xiàn)在曬出來與大家分享,感興趣的朋友可以了解小哦2013-01-01Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02丟失Android系統(tǒng)庫或者Conversion to Dalvik format failed with error
這篇文章主要介紹了丟失Android系統(tǒng)庫或者Conversion to Dalvik format failed with error 1錯(cuò)誤的解決方法,分析了Android系統(tǒng)庫丟失及版本問題的處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12深入理解Android中的Window和WindowManager
這篇文章給大家介紹了Window和WindowManager知識,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-02-02Android實(shí)現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09