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

Android仿京东首页秒杀倒计时

程序员文章站 2022-06-23 15:13:22
本文实例为大家分享了android仿京东首页秒杀倒计时的具体代码,供大家参考,具体内容如下 xml配置

本文实例为大家分享了android仿京东首页秒杀倒计时的具体代码,供大家参考,具体内容如下

xml配置

<linearlayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#ffffff"
  android:orientation="vertical">
  <linearlayout
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center_vertical">
  <textview
   android:id="@+id/tv_miaosha"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginleft="8dp"
   android:text="京东秒杀"
   android:textcolor="#f00"
   android:textsize="20sp" />
  <textview
   android:id="@+id/tv_miaosha_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="12点场"
   android:textsize="20sp" />
  <linearlayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <textview
   android:id="@+id/tv_miaosha_shi"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textcolor="#fff"
   android:textsize="15sp" />
   <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="3dp"
   android:text=":" />
   <textview
   android:id="@+id/tv_miaosha_minter"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textcolor="#fff"
   android:textsize="15sp" />
   <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="3dp"
   android:text=":" />
   <textview
   android:id="@+id/tv_miaosha_second"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textcolor="#fff"
   android:textsize="15sp" />
  </linearlayout>
 </linearlayout>
</linearlayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <solid android:color="#000"></solid>
 <corners android:radius="2.5dp"></corners>
</shape>

方法内容:

//得到控件
 mmiaoshashitv = view.findviewbyid(r.id.tv_miaosha_shi);
 mmiaoshatimetv = view.findviewbyid(r.id.tv_miaosha_time);
 mmiaoshamintertv = view.findviewbyid(r.id.tv_miaosha_minter);
 mmiaoshasecondtv = view.findviewbyid(r.id.tv_miaosha_second);



 /****************京东秒杀倒计时**********************/

 //使用handler用于更新ui
 private handler handler = new handler() {
 @override
 public void handlemessage(message msg) {
  super.handlemessage(msg);
  countdown();
  handler.sendemptymessagedelayed(0, 1000);
 }
 };

 /**
 * 秒杀
 */
 private void countdown() {
 simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");
 date curdate = new date(system.currenttimemillis());
 string format = df.format(curdate);
 stringbuffer buffer = new stringbuffer();
 string substring = format.substring(0, 11);
 buffer.append(substring);
 log.d("ccc", substring);
 calendar calendar = calendar.getinstance();
 int hour = calendar.get(calendar.hour_of_day);
 if (hour % 2 == 0) {
  mmiaoshatimetv.settext(hour + "点场");
  buffer.append((hour + 2));
  buffer.append(":00:00");
 } else {
  mmiaoshatimetv.settext((hour - 1) + "点场");
  buffer.append((hour + 1));
  buffer.append(":00:00");
 }
 string totime = buffer.tostring();
 try {
  java.util.date date = df.parse(totime);
  java.util.date date1 = df.parse(format);
  long defferenttime = date.gettime() - date1.gettime();
  long days = defferenttime / (1000 * 60 * 60 * 24);
  long hours = (defferenttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
  long minute = (defferenttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
  long seconds = defferenttime % 60000;
  long second = math.round((float) seconds / 1000);
  mmiaoshashitv.settext("0" + hours + "");
  if (minute >= 10) {
  mmiaoshamintertv.settext(minute + "");
  } else {
  mmiaoshamintertv.settext("0" + minute + "");
  }
  if (second >= 10) {
  mmiaoshasecondtv.settext(second + "");
  } else {
  mmiaoshasecondtv.settext("0" + second + "");
  }
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }

 /***************京东秒杀倒计时****************/


开启倒计时:

handler.sendemptymessage(0);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。