国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android定時器..做天數時間倒計時

2019-11-08 00:19:08
字體:
來源:轉載
供稿:網友

如圖: 

主要的倒計時代碼如下....從秒判斷起...再判斷分...小時...

    /**     * 倒計時計算     */    PRivate void computeTime() {        mSecond--;        if (mSecond < 0) {            mMin--;            mSecond = 59;            if (mMin < 0) {                mMin = 59;                mHour--;                if (mHour < 0) {                    // 倒計時結束                    mHour = 23;                    mDay--;                    if(mDay < 0){                        // 倒計時結束                        mDay = 0;                        mHour= 0;                        mMin = 0;                        mSecond = 0;                    }                }            }        }    }

定時器主要代碼如下...當然也可以開線程或者開后臺服務來處理...只是沒那種必要...定時器就可以搞定容易控制...畢竟倒計時時間起點...你總得后臺獲取吧,不是做時鐘鬧鐘...如果是做時鐘鬧鐘...拿你也不用考慮后臺服務或者自己開現場...而是使用AlarmManager來實現

    /**     * 開啟倒計時     *  //time為Date類型:在指定時間執行一次。     *        timer.schedule(task, time);     *  //firstTime為Date類型,period為long,表示從firstTime時刻開始,每隔period毫秒執行一次。     *       timer.schedule(task, firstTime,period);     *  //delay 為long類型:從現在起過delay毫秒執行一次。     *       timer.schedule(task, delay);     *  //delay為long,period為long:從現在起過delay毫秒以后,每隔period毫秒執行一次。     *       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+"");//天數不用補位                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,,前面補位一個"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;    // 倒計時    private TextView mDays_Tv, mHours_Tv, mMinutes_Tv, mSeconds_Tv;    private long mDay = 23;// 天    private long mHour = 11;//小時,    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+"");//天數不用補位                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,,前面補位一個"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();    }    /**     * 開啟倒計時     *  //time為Date類型:在指定時間執行一次。     *        timer.schedule(task, time);     *  //firstTime為Date類型,period為long,表示從firstTime時刻開始,每隔period毫秒執行一次。     *       timer.schedule(task, firstTime,period);     *  //delay 為long類型:從現在起過delay毫秒執行一次。     *       timer.schedule(task, delay);     *  //delay為long,period為long:從現在起過delay毫秒以后,每隔period毫秒執行一次。     *       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);    }    /**     * 倒計時計算     */    private void computeTime() {        mSecond--;        if (mSecond < 0) {            mMin--;            mSecond = 59;            if (mMin < 0) {                mMin = 59;                mHour--;                if (mHour < 0) {                    // 倒計時結束                    mHour = 23;                    mDay--;                    if(mDay < 0){                        // 倒計時結束                        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="距離開團還有"            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>好了....倒計時完美實現


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵台县| 缙云县| 洛浦县| 昌宁县| 万安县| 自贡市| 柳江县| 来凤县| 浦北县| 太仆寺旗| 观塘区| 炎陵县| 垫江县| 长葛市| 旬阳县| 清丰县| 桃园市| 贵南县| 泊头市| 清远市| 连南| 扶风县| 麻江县| 遂昌县| 北票市| 泌阳县| 新和县| 大宁县| 宝应县| 会同县| 齐齐哈尔市| 辛集市| 湘阴县| 林州市| 莱西市| 盘山县| 荆州市| 平顺县| 吴江市| 正阳县| 新昌县|