android倒計(jì)時實(shí)現(xiàn)

新的商品詳情,對特賣要增加倒計(jì)時的功能:

創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營銷,提供網(wǎng)站制作、成都網(wǎng)站建設(shè)、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營銷、成都小程序開發(fā)、公眾號商城、等建站開發(fā),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢。

采用了android系統(tǒng)的CountDownTimer這個類

關(guān)于這個類的用法:

 * Schedule a countdown until a time in the future, with

 * regular notifications on intervals along the way.

 *

 * Example of showing a 30 second countdown in a text field:

 *

 * <pre class="prettyprint">

 * new CountDownTimer(30000, 1000) {

 *

 *     public void onTick(long millisUntilFinished) {

 *         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

 *     }

 *

 *     public void onFinish() {

 *         mTextField.setText("done!");

 *     }

 *  }.start();

在時間工具類中加了兩個方法:

 /**

     * 獲得倒計(jì)時的時間間隔

     * 

     * @param dateStr

     * @return

     */

    public static long getCountdownInterval(String dateStr) {

        long interval = 0;

        if (StringUtils.isBlank(dateStr)) {

            return interval;

        }

        Date countdownDate = new Date();

        try {

            countdownDate = DB_DATE_FORMAT.parse(dateStr);

        } catch (ParseException e) {

            LogUtils.loge("DateUtils", "日期格式有問題");

        }

        Date now = new Date();

        interval = countdownDate.getTime() - now.getTime();

        if (interval < 0)

            return 0;

        return interval;

    }

 /**

     * @param createTime

     * @return 返回離當(dāng)前時刻的時間間隔,以中文形式描述

     */

    public static int[] getCountdownIntervalDetails(long intervalSec) {

        int[] countdownIntervalDetals = new int[4];

        intervalSec = intervalSec / 1000;// 先轉(zhuǎn)成秒

        // 天

        int day = (int) (intervalSec / (24 * 3600));

        countdownIntervalDetals[0] = day;

        intervalSec = intervalSec - day * (24 * 3600);

        // 時

        int hour = (int) (intervalSec / 3600);

        countdownIntervalDetals[1] = hour;

        intervalSec = intervalSec - hour * 3600;

        // 分

        int min = (int) (intervalSec / 60);

        countdownIntervalDetals[2] = min;

        intervalSec = intervalSec - min * 60;

        countdownIntervalDetals[3] = (int) intervalSec;

        return countdownIntervalDetals;

    }

最后,組裝成一個控件:

/**

 * 倒計(jì)時控件

 */

public class L_CountdownBlock implements Y_UIBlock {

    private Context mContext;

    private View container;

    private CountDownTimer cdt;

    private TextView tv_countdown_day;

    private TextView tv_countdown_hour;

    private TextView tv_countdown_min;

    private TextView tv_countdown_sec;

    protected int[] intervalDetails;

    public L_CountdownBlock(View view) {

        this.container = view;

        this.mContext = container.getContext();

    }

    /**

     * 設(shè)置結(jié)束時間

     * 

     * @param dateStr

     */

    private void setDate(String dateStr) {

        long interval = DateUtils.getCountdownInterval(dateStr);

        cdt = new CountDownTimer(interval, 1000) {

            @Override

            public void onTick(long millisUntilFinished) {

                intervalDetails = DateUtils

                        .getCountdownIntervalDetails(millisUntilFinished);

                tv_countdown_day.setText(intervalDetails[0] + "");

                tv_countdown_hour.setText(intervalDetails[1] + "");

                tv_countdown_min.setText(intervalDetails[2] + "");

                tv_countdown_sec.setText(intervalDetails[3] + "");

            }

            @Override

            public void onFinish() {

            //TODO 加了倒計(jì)時結(jié)束的邏輯

            }

        };

        cdt.start();

    }

    /**

     * 取消倒計(jì)時

     */

    private void cancelCountdown() {

        if (null != cdt) {

            cdt.cancel();

        }

    }

    @Override

    public <T> void update(T t) {

        tv_countdown_day = (TextView) container

                .findViewById(R.id.tv_countdown_day);

        tv_countdown_hour = (TextView) container

                .findViewById(R.id.tv_countdown_hour);

        tv_countdown_min = (TextView) container

                .findViewById(R.id.tv_countdown_min);

        tv_countdown_sec = (TextView) container

                .findViewById(R.id.tv_countdown_sec);

        setDate((String) t);

    }

    @Override

    public View getContentView() {

        return container;

    }

}

本文名稱:android倒計(jì)時實(shí)現(xiàn)
網(wǎng)址分享:http://muchs.cn/article14/ghggge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)全網(wǎng)營銷推廣、自適應(yīng)網(wǎng)站、移動網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)