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

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

服务器之家 - 编程语言 - Android - Android实现淘宝倒计时功能

Android实现淘宝倒计时功能

2022-09-26 14:52没心没肺没网名 Android

这篇文章主要为大家详细介绍了Android实现淘宝倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现淘宝倒计时的具体代码,供大家参考,具体内容如下

一、效果图(这里为了方便我就没弄gif图了,功能是能动的)

Android实现淘宝倒计时功能

二、实现步骤

1.自定义倒计时控件、

?
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.cqxxny.myapplication;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.Timer;
import java.util.TimerTask;
/**
 * 功 能: 倒计时工具类
 * 所属模块:
 * 创建时间: 2018/11/15
 * 功能描述:
 */
@SuppressLint("HandlerLeak")
public class RushBuyCountDownTimerView extends LinearLayout {
 
 // 小时,十位
 private TextView tv_hour_decade;
 // 小时,个位
 private TextView tv_hour_unit;
 // 分钟,十位
 private TextView tv_min_decade;
 // 分钟,个位
 private TextView tv_min_unit;
 // 秒,十位
 private TextView tv_sec_decade;
 // 秒,个位
 private TextView tv_sec_unit;
 
 private Context context;
 
 private int hour_decade;
 private int hour_unit;
 private int min_decade;
 private int min_unit;
 private int sec_decade;
 private int sec_unit;
 // 计时器
 private Timer timer;
 
 private Handler handler = new Handler() {
 
 public void handleMessage(Message msg) {
 countDown();
 };
 };
 
 public RushBuyCountDownTimerView(Context context, AttributeSet attrs) {
 super(context, attrs);
 
 this.context = context;
 LayoutInflater inflater = (LayoutInflater) context
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = inflater.inflate(R.layout.time, this);
 
 tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade);
 tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit);
 tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade);
 tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit);
 tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade);
 tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit);
 
 }
 
 /**
 *
 * @Description: 开始计时
 * @param
 * @return void
 * @throws
 */
 public void start() {
 
 if (timer == null) {
 timer = new Timer();
 timer.schedule(new TimerTask() {
 
 @Override
 public void run() {
  handler.sendEmptyMessage(0);
 }
 }, 0, 1000);
 }
 }
 
 /**
 *
 * @Description: 停止计时
 * @param
 * @return void
 * @throws
 */
 public void stop() {
 if (timer != null) {
 timer.cancel();
 timer = null;
 }
 }
 
 /**
 * @throws Exception
 *
 * @Description: 设置倒计时的时长
 * @param
 * @return void
 * @throws
 */
 public void setTime(int hour, int min, int sec) {
 
 if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0
 || sec < 0) {
 throw new RuntimeException("Time format is error,please check out your code");
 }
 
 hour_decade = hour / 10;
 hour_unit = hour - hour_decade * 10;
 
 min_decade = min / 10;
 min_unit = min - min_decade * 10;
 
 sec_decade = sec / 10;
 sec_unit = sec - sec_decade * 10;
 
 tv_hour_decade.setText(hour_decade + "");
 tv_hour_unit.setText(hour_unit + "");
 tv_min_decade.setText(min_decade + "");
 tv_min_unit.setText(min_unit + "");
 tv_sec_decade.setText(sec_decade + "");
 tv_sec_unit.setText(sec_unit + "");
 
 }
 
 /**
 *
 * @Description: 倒计时
 * @param
 * @return boolean
 * @throws
 */
 private void countDown() {
 
 if (isCarry4Unit(tv_sec_unit)) {
 if (isCarry4Decade(tv_sec_decade)) {
 
 if (isCarry4Unit(tv_min_unit)) {
  if (isCarry4Decade(tv_min_decade)) {
 
  if (isCarry4Unit(tv_hour_unit)) {
  if (isCarry4Decade(tv_hour_decade)) {
  Toast.makeText(context, "时间到了",
   Toast.LENGTH_SHORT).show();
  stop();
  }
  }
  }
 }
 }
 }
 }
 
 /**
 *
 * @Description: 变化十位,并判断是否需要进位
 * @param
 * @return boolean
 * @throws
 */
 private boolean isCarry4Decade(TextView tv) {
 
 int time = Integer.valueOf(tv.getText().toString());
 time = time - 1;
 if (time < 0) {
 time = 5;
 tv.setText(time + "");
 return true;
 } else {
 tv.setText(time + "");
 return false;
 }
 
 }
 
 /**
 *
 * @Description: 变化个位,并判断是否需要进位
 * @param
 * @return boolean
 * @throws
 */
 private boolean isCarry4Unit(TextView tv) {
 
 int time = Integer.valueOf(tv.getText().toString());
 time = time - 1;
 if (time < 0) {
 time = 9;
 tv.setText(time + "");
 return true;
 } else {
 tv.setText(time + "");
 return false;
 }
 
 }
}

2.自定义控件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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="horizontal">
 
 <TextView
  android:id="@+id/tv_hour_decade"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:padding="5dp"
  android:text="0"
  android:textColor="#ffffff"
  android:textSize="20dp" />
 
 <TextView
  android:id="@+id/tv_hour_unit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="2dp"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:padding="5dp"
  android:text="0"
  android:textColor="#ffffff"
  android:textSize="20dp" />
 
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text=":"
  android:padding="5dp"
  android:textColor="#454545"
  android:textSize="20dp" />
 
 <TextView
  android:id="@+id/tv_min_decade"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:text="0"
  android:padding="5dp"
  android:textColor="#ffffff"
  android:textSize="20dp"/>
 
 <TextView
  android:id="@+id/tv_min_unit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="1dp"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:padding="5dp"
  android:text="0"
  android:textColor="#ffffff"
  android:textSize="20dp"/>
 
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text=":"
  android:padding="5dp"
  android:textColor="#454545"
  android:textSize="20dp" />
 
 
 <TextView
  android:id="@+id/tv_sec_decade"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:text="0"
  android:padding="5dp"
  android:textColor="#ffffff"
  android:textSize="20dp"/>
 
 <TextView
  android:id="@+id/tv_sec_unit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="1dp"
  android:background="@drawable/sousuo"
  android:gravity="center"
  android:padding="5dp"
  android:text="0"
  android:textColor="#ffffff"
  android:textSize="20dp" />
 
</LinearLayout>

3.自定义控件转圆角、

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 
 <!-- 背景颜色 -->
 <solid android:color="#454545" />
 
 
 <!-- 控制边界线颜色和大小 -->
 <stroke
  android:width="1dp"
  android:color="#454545" />
 
 <!-- 控制圆角大小 -->
 <corners android:radius="5dp" />
 
</shape>

4.activity代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.cqxxny.myapplication;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity {
 /**
  * 功 能: 倒计时
  * 所属模块:
  * 创建时间: 2018/11/15
  * 功能描述:
  */
 private RushBuyCountDownTimerView timerView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  timerView = (RushBuyCountDownTimerView) findViewById(R.id.timerView);
  // 设置时间(hour,min,sec)
  timerView.setTime(10, 0, 10);
  // 开始倒计时
  timerView.start();
 }
}

5.activity的xml布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 tools:context=".MainActivity">
 
 <com.cqxxny.myapplication.RushBuyCountDownTimerView
  android:id="@+id/timerView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></com.cqxxny.myapplication.RushBuyCountDownTimerView>
 
</LinearLayout>

源码下载:Android实现淘宝倒计时

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

原文链接:https://blog.csdn.net/Android_Cll/article/details/84100726

延伸 · 阅读

精彩推荐