最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 實(shí)現(xiàn)搶購倒計(jì)時功能的示例

 更新時間:2021年03月24日 10:53:35   作者:龍旋  
這篇文章主要介紹了Android 實(shí)現(xiàn)搶購倒計(jì)時功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下

一、效果圖

二、思路

算多少秒,秒數(shù)取余60,(滿足分后剩下的秒數(shù))
算多少分,秒數(shù)除60,再取余60 (總分?jǐn)?shù)滿足小時后剩下的分?jǐn)?shù))
算多少時,秒數(shù)除60,除60,再取余24 (總小時滿足天后剩下的小時)
算多少天,秒數(shù)除60,除60,除24 等到的整數(shù)就是天數(shù)

三、實(shí)現(xiàn)步驟:

我們這里的時間格式為后臺返回,格式為:

2021-12-24 00:00:00

1、時間轉(zhuǎn)換的工具類

 //將年-月-天 時:分:秒轉(zhuǎn)化為毫秒格式
 public static long residueTimeout(String endDate, String newDate) throws ParseException {

  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date d1 = df.parse(endDate);
  Date d2 = df.parse(newDate);
  long diff = d1.getTime() - d2.getTime();

  return diff;
 }

 /*
  * 將毫秒轉(zhuǎn)換成時間戳
  */
 public static String stampToDate(Long s) {
  String res;
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date date = new Date(s);
  res = simpleDateFormat.format(date);
  return res;
 }

2、時間倒計(jì)時工具類

package com.sjl.keeplive.huawei;
import android.os.CountDownTimer;

/**
 * 倒計(jì)時工具類
 */
public class CountDownTimerUtils {
 /**
  * 倒計(jì)時結(jié)束的回調(diào)接口
  */
 public interface FinishDelegate {
  void onFinish();
 }

 /**
  * 定期回調(diào)的接口
  */
 public interface TickDelegate {
  void onTick(long pMillisUntilFinished);
 }

 private final static long ONE_SECOND = 1000;
 /**
  * 總倒計(jì)時時間
  */
 private long mMillisInFuture = 0;
 /**
  * 定期回調(diào)的時間 必須大于0 否則會出現(xiàn)ANR
  */
 private long mCountDownInterval;
 /**
  * 倒計(jì)時結(jié)束的回調(diào)
  */
 private FinishDelegate mFinishDelegate;
 /**
  * 定期回調(diào)
  */
 private TickDelegate mTickDelegate;
 private MyCountDownTimer mCountDownTimer;

 /**
  * 獲取 CountDownTimerUtils
  *
  * @return CountDownTimerUtils
  */
 public static CountDownTimerUtils getCountDownTimer() {
  return new CountDownTimerUtils();
 }

 /**
  * 設(shè)置定期回調(diào)的時間 調(diào)用{@link #setTickDelegate(TickDelegate)}
  *
  * @param pCountDownInterval 定期回調(diào)的時間 必須大于0
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setCountDownInterval(long pCountDownInterval) {
  this.mCountDownInterval = pCountDownInterval;
  return this;
 }

 /**
  * 設(shè)置倒計(jì)時結(jié)束的回調(diào)
  *
  * @param pFinishDelegate 倒計(jì)時結(jié)束的回調(diào)接口
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setFinishDelegate(FinishDelegate pFinishDelegate) {
  this.mFinishDelegate = pFinishDelegate;
  return this;
 }

 /**
  * 設(shè)置總倒計(jì)時時間
  *
  * @param pMillisInFuture 總倒計(jì)時時間
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setMillisInFuture(long pMillisInFuture) {
  this.mMillisInFuture = pMillisInFuture;
  return this;
 }

 /**
  * 設(shè)置定期回調(diào)
  *
  * @param pTickDelegate 定期回調(diào)接口
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setTickDelegate(TickDelegate pTickDelegate) {
  this.mTickDelegate = pTickDelegate;
  return this;
 }

 public void create() {
  if (mCountDownTimer != null) {
   mCountDownTimer.cancel();
   mCountDownTimer = null;
  }
  if (mCountDownInterval <= 0) {
   mCountDownInterval = mMillisInFuture + ONE_SECOND;
  }
  mCountDownTimer = new MyCountDownTimer(mMillisInFuture, mCountDownInterval);
  mCountDownTimer.setTickDelegate(mTickDelegate);
  mCountDownTimer.setFinishDelegate(mFinishDelegate);
 }

 /**
  * 開始倒計(jì)時
  */
 public void start() {
  if (mCountDownTimer == null) {
   create();
  }
  mCountDownTimer.start();
 }

 /**
  * 取消倒計(jì)時
  */
 public void cancel() {
  if (mCountDownTimer != null) {
   mCountDownTimer.cancel();
  }
 }

 private static class MyCountDownTimer extends CountDownTimer {
  private FinishDelegate mFinishDelegate;
  private TickDelegate mTickDelegate;

  /**
   * @param millisInFuture The number of millis in the future from the call
   *       to {@link #start()} until the countdown is done and {@link #onFinish()}
   *       is called.
   * @param countDownInterval The interval along the way to receive
   *       {@link #onTick(long)} callbacks.
   */
  public MyCountDownTimer(long millisInFuture, long countDownInterval) {
   super(millisInFuture, countDownInterval);
  }

  @Override
  public void onTick(long millisUntilFinished) {
   if (mTickDelegate != null) {
    mTickDelegate.onTick(millisUntilFinished);
   }
  }

  @Override
  public void onFinish() {
   if (mFinishDelegate != null) {
    mFinishDelegate.onFinish();
   }
  }

  void setFinishDelegate(FinishDelegate pFinishDelegate) {
   this.mFinishDelegate = pFinishDelegate;
  }

  void setTickDelegate(TickDelegate pTickDelegate) {
   this.mTickDelegate = pTickDelegate;
  }
 }
}

3、布局文件

 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:orientation="horizontal">

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="搶購倒計(jì)時:" />

  <TextView
   android:id="@+id/text_day"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 天 " />

  <TextView
   android:id="@+id/text_hour"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 時 " />

  <TextView
   android:id="@+id/text_minute"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 分 " />

  <TextView
   android:id="@+id/text_second"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 秒 " />

 </LinearLayout>

4、倒計(jì)時顯示處理

 public static void liveDescCountTime(long ms, TextView tvDays, TextView tvHour, TextView tvMinutes, TextView tvSeconds) {
  long totalSeconds = ms / 1000;
  long seconds = totalSeconds % 60;
  long minutes = totalSeconds / 60 % 60;
  long hours = totalSeconds / 60 / 60 % 24;
  long days = totalSeconds / 60 / 60 / 24;

  String dayStr = "";
  if (days > 0) {
   if (days > 9) {
    dayStr += days + "";
   } else if (days > 0) {
    dayStr += "0" + days + "";
   } else {
    dayStr += "00";
   }
  } else {
   dayStr = "00";
  }
  tvDays.setText(dayStr);

  String hourStr = "";
  if (hours > 0) {
   if (hours > 9) {
    hourStr += hours + "";
   } else if (hours > 0) {
    hourStr += "0" + hours + "";
   } else {
    hourStr += "00";
   }
  } else {
   hourStr = "00";
  }
  tvHour.setText(hourStr);

  String minutesStr = "";
  if (minutes > 0) {
   if (minutes > 9) {
    minutesStr += minutes + "";
   } else if (minutes > 0) {
    minutesStr += "0" + minutes + "";
   } else {
    minutesStr += "00";
   }
  } else {
   minutesStr = "00";
  }
  tvMinutes.setText(minutesStr);

  String secondStr = "";
  if (minutes > 0) {
   if (seconds > 9) {
    secondStr += seconds;
   } else if (seconds > 0) {
    secondStr += "0" + seconds;
   } else {
    secondStr += "00";
   }
  } else {
   secondStr = "00";
  }
  tvSeconds.setText(secondStr);
 }

5、開始倒計(jì)時

        final TextView text_day = findViewById(R.id.text_day);
  final TextView text_hour = findViewById(R.id.text_hour);
  final TextView text_minute = findViewById(R.id.text_minute);
  final TextView text_second = findViewById(R.id.text_second);

  long residueTime = 0;
        //獲取當(dāng)前時間
  String stampToDate = stampToDate(System.currentTimeMillis());
  try {
            //2021-12-24 00:00:00為模擬倒計(jì)時間數(shù)據(jù)
   residueTime = residueTimeout("2021-12-24 00:00:00", stampToDate);
  } catch (ParseException e) {
   e.printStackTrace();
  }

        //倒計(jì)時
  CountDownTimerUtils.getCountDownTimer()
    .setMillisInFuture(residueTime)
    .setCountDownInterval(1000)
    .setTickDelegate(new CountDownTimerUtils.TickDelegate() {
     @Override
     public void onTick(long pMillisUntilFinished) {
      liveDescCountTime(pMillisUntilFinished, text_day, text_hour, text_minute, text_second);
     }
    })
    .setFinishDelegate(new CountDownTimerUtils.FinishDelegate() {
     @Override
     public void onFinish() {
      //倒計(jì)時完成后處理
     }
    }).start();

以上就是Android 實(shí)現(xiàn)搶購倒計(jì)時功能的示例的詳細(xì)內(nèi)容,更多關(guān)于Android 搶購倒計(jì)時功能的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android Studio配置本地SDK的方法

    Android Studio配置本地SDK的方法

    這篇文章主要介紹了Android Studio配置本地SDK的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android圖片緩存之Lru算法(二)

    Android圖片緩存之Lru算法(二)

    LRU緩存簡單的說就是緩存一定量的數(shù)據(jù),當(dāng)超過設(shè)定的閾值時就把一些過期的數(shù)據(jù)刪除掉,這篇文章主要介紹了Android圖片緩存Lru算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android仿QQ空間動態(tài)界面分享功能

    Android仿QQ空間動態(tài)界面分享功能

    這篇文章主要介紹了Android仿QQ空間動態(tài)界面分享功能,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-04-04
  • Android TextView高級顯示技巧實(shí)例小結(jié)

    Android TextView高級顯示技巧實(shí)例小結(jié)

    這篇文章主要介紹了Android TextView高級顯示技巧,結(jié)合實(shí)例形式總結(jié)分析了Android TextView控件進(jìn)行文字與圖片顯示的相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-11-11
  • Google大佬都用的廣播goAsync源碼分析

    Google大佬都用的廣播goAsync源碼分析

    這篇文章主要為大家介紹了Google大佬都用的廣播?goAsync源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android 實(shí)現(xiàn)高斯模糊效果且兼容低版本

    Android 實(shí)現(xiàn)高斯模糊效果且兼容低版本

    這篇文章主要介紹了Android 實(shí)現(xiàn)高斯模糊效果且兼容低版本的相關(guān)資料,本文圖文并茂介紹的非常詳細(xì),需要的朋友可以參考下
    2016-09-09
  • 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解

    在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解

    這篇文章主要介紹了在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解,消息推送功能可以說移動APP不可缺少的功能之一,使用WebSocket實(shí)現(xiàn)消息推送功能。感興趣的可以了解一下
    2020-07-07
  • Android實(shí)現(xiàn)PDF預(yù)覽打印功能

    Android實(shí)現(xiàn)PDF預(yù)覽打印功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)PDF預(yù)覽打印功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 你值得擁有的Android Studio開發(fā)小技巧

    你值得擁有的Android Studio開發(fā)小技巧

    這篇文章主要為大家分享了值得擁有的Android Studio開發(fā)小技巧,介紹幾個比較好用的技巧和快捷鍵,提升我們的編碼效率,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評論

富平县| 南雄市| 乌拉特前旗| 静乐县| 赤峰市| 工布江达县| 莱州市| 西贡区| 天长市| 观塘区| 新源县| 延津县| 延川县| 子长县| 景东| 赣州市| 仲巴县| 绥芬河市| 惠水县| 石城县| 德清县| 枣庄市| 孝昌县| 莫力| 长宁区| 大丰市| 九龙县| 九寨沟县| 封丘县| 扎兰屯市| 通许县| 广饶县| 邯郸市| 金昌市| 大姚县| 靖边县| 宜兰市| 格尔木市| 仁布县| 介休市| 绿春县|