欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现抢购倒计时功能的示例

程序员文章站 2022-06-25 09:28:52
一、效果图二、思路算多少秒,秒数取余60,(满足分后剩下的秒数)算多少分,秒数除60,再取余60 (总分数满足小时后剩下的分数)算多少时,秒数除60,除60,再取余24 (总小时满足天后剩下的小时)算...

一、效果图

Android 实现抢购倒计时功能的示例

二、思路

算多少秒,秒数取余60,(满足分后剩下的秒数)
算多少分,秒数除60,再取余60 (总分数满足小时后剩下的分数)
算多少时,秒数除60,除60,再取余24 (总小时满足天后剩下的小时)
算多少天,秒数除60,除60,除24 等到的整数就是天数

三、实现步骤:

我们这里的时间格式为后台返回,格式为:

2021-12-24 00:00:00

1、时间转换的工具类

 //将年-月-天 时:分:秒转化为毫秒格式
 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;
 }

 /*
  * 将毫秒转换成时间戳
  */
 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、时间倒计时工具类

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

/**
 * 倒计时工具类
 */
public class countdowntimerutils {
 /**
  * 倒计时结束的回调接口
  */
 public interface finishdelegate {
  void onfinish();
 }

 /**
  * 定期回调的接口
  */
 public interface tickdelegate {
  void ontick(long pmillisuntilfinished);
 }

 private final static long one_second = 1000;
 /**
  * 总倒计时时间
  */
 private long mmillisinfuture = 0;
 /**
  * 定期回调的时间 必须大于0 否则会出现anr
  */
 private long mcountdowninterval;
 /**
  * 倒计时结束的回调
  */
 private finishdelegate mfinishdelegate;
 /**
  * 定期回调
  */
 private tickdelegate mtickdelegate;
 private mycountdowntimer mcountdowntimer;

 /**
  * 获取 countdowntimerutils
  *
  * @return countdowntimerutils
  */
 public static countdowntimerutils getcountdowntimer() {
  return new countdowntimerutils();
 }

 /**
  * 设置定期回调的时间 调用{@link #settickdelegate(tickdelegate)}
  *
  * @param pcountdowninterval 定期回调的时间 必须大于0
  * @return countdowntimerutils
  */
 public countdowntimerutils setcountdowninterval(long pcountdowninterval) {
  this.mcountdowninterval = pcountdowninterval;
  return this;
 }

 /**
  * 设置倒计时结束的回调
  *
  * @param pfinishdelegate 倒计时结束的回调接口
  * @return countdowntimerutils
  */
 public countdowntimerutils setfinishdelegate(finishdelegate pfinishdelegate) {
  this.mfinishdelegate = pfinishdelegate;
  return this;
 }

 /**
  * 设置总倒计时时间
  *
  * @param pmillisinfuture 总倒计时时间
  * @return countdowntimerutils
  */
 public countdowntimerutils setmillisinfuture(long pmillisinfuture) {
  this.mmillisinfuture = pmillisinfuture;
  return this;
 }

 /**
  * 设置定期回调
  *
  * @param ptickdelegate 定期回调接口
  * @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);
 }

 /**
  * 开始倒计时
  */
 public void start() {
  if (mcountdowntimer == null) {
   create();
  }
  mcountdowntimer.start();
 }

 /**
  * 取消倒计时
  */
 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="抢购倒计时:" />

  <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、倒计时显示处理

 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、开始倒计时

        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;
        //获取当前时间
  string stamptodate = stamptodate(system.currenttimemillis());
  try {
            //2021-12-24 00:00:00为模拟倒计时间数据
   residuetime = residuetimeout("2021-12-24 00:00:00", stamptodate);
  } catch (parseexception e) {
   e.printstacktrace();
  }

        //倒计时
  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() {
      //倒计时完成后处理
     }
    }).start();

以上就是android 实现抢购倒计时功能的示例的详细内容,更多关于android 抢购倒计时功能的资料请关注其它相关文章!