Android限时抢购倒计时实现代码
作者:DW的dory 时间:2021-08-09 13:56:51
限时抢购倒计时实现效果图
布局:
<LinearLayout
android:id="@+id/ll_xsqg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="14sp"
android:text="@string/xsqg"/>
<TextView
android:id="@+id/tv_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/time_corner"
android:textColor="@android:color/white"
android:textSize="12sp"
android:text="02"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text=":"/>
<TextView
android:id="@+id/tv_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/time_corner"
android:textColor="@android:color/white"
android:textSize="12sp"
android:text="15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text=":"/>
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/time_corner"
android:textColor="@android:color/white"
android:textSize="12sp"
android:text="36"/>
</LinearLayout>
代码实现
public class HomeActivity extends Activity {
@Bind(R.id.tv_hour)
TextView tvHour;
@Bind(R.id.tv_minute)
TextView tvMinute;
@Bind(R.id.tv_second)
TextView tvSecond;
private long mHour = 02;
private long mMin = 15;
private long mSecond = 36;
private boolean isRun = true;
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1) {
computeTime();
if (mHour<10){
tvHour.setText("0"+mHour+"");
}else {
tvHour.setText("0"+mHour+"");
}
if (mMin<10){
tvMinute.setText("0"+mMin+"");
}else {
tvMinute.setText(mMin+"");
}
if (mSecond<10){
tvSecond.setText("0"+mSecond+"");
}else {
tvSecond.setText(mSecond+"");
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
startRun();
}
/**
* 开启倒计时
*/
private void startRun() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRun) {
try {
Thread.sleep(1000); // sleep 1000ms
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
/**
* 倒计时计算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
}
}
}
}
标签:Android,倒计时
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实战之敏感词过滤器
2022-01-18 10:32:03
![](https://img.aspxhome.com/file/2023/2/83092_0s.png)
C#获取文件夹所占空间大小的功能
2022-12-02 10:54:15
Java8 Lambda表达式详解及实例
2023-02-06 20:59:15
init output stream初始化输出流源码分析
2023-01-08 09:53:20
![](https://img.aspxhome.com/file/2023/8/90188_0s.jpg)
关于Springboot中JSCH的使用及说明
2023-11-28 02:32:16
如何使用Java爬虫批量爬取图片
2023-01-11 16:19:54
![](https://img.aspxhome.com/file/2023/4/114054_0s.png)
C# Socket连接请求超时机制实现代码分享
2021-10-26 06:52:05
Java编程实现轨迹压缩之Douglas-Peucker算法详细代码
2023-11-29 15:25:47
![](https://img.aspxhome.com/file/2023/6/59486_0s.png)
java如何使用Lombok更优雅地编码
2022-07-24 23:24:50
详解基于java的Socket聊天程序——客户端(附demo)
2021-06-28 04:19:54
![](https://img.aspxhome.com/file/2023/6/114036_0s.png)
C#实现聊天窗体以及抖动
2021-06-07 10:40:50
java 实现约瑟夫环的实例代码
2022-06-24 16:26:11
java中常见的死锁以及解决方法代码
2023-04-07 19:47:30
![](https://img.aspxhome.com/file/2023/2/64422_0s.png)
java中的控制结构(if,循环)详解
2022-05-13 19:18:59
详解Java的Hibernat框架中的Map映射与SortedMap映射
2021-08-21 20:31:59
JDK8 中Arrays.sort() 排序方法详解
2023-01-17 10:38:30
![](https://img.aspxhome.com/file/2023/2/79892_0s.png)
解决Unity无限滚动复用列表的问题
2022-12-26 04:42:18
![](https://img.aspxhome.com/file/2023/2/111292_0s.gif)
java web实现分页查询实例方法
2022-12-18 18:07:57
![](https://img.aspxhome.com/file/2023/9/72399_0s.png)
Springcloud Eureka配置及集群代码实例
2021-09-19 05:54:52
java 回调机制的实例详解
2023-12-04 10:03:04