服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android仿京东首页秒杀倒计时

Android仿京东首页秒杀倒计时

2022-09-19 17:53拉莫帅 Android

这篇文章主要为大家详细介绍了Android仿京东首页秒杀倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<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>
?
1
2
3
4
5
6
<?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>

方法内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//得到控件
 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();
 }
 }
 
 /***************京东秒杀倒计时****************/

开启倒计时:

?
1
handler.sendEmptyMessage(0);

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

原文链接:https://blog.csdn.net/chen_md/article/details/79999852

延伸 · 阅读

精彩推荐