Android限时抢购倒计时实现代码

限时抢购倒计时实现效果图

布局:

<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--; } } } }

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

时间: 2024-07-31 09:06:20

Android限时抢购倒计时实现代码的相关文章

javascript电商网站抢购倒计时效果实现_javascript技巧

本文实例讲述了javascript电商网站抢购倒计时效果实现代码.分享给大家供大家参考.具体如下: 运行效果截图如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>团购--限时抢</title> </head> &

基于javascript实现精确到毫秒的倒计时限时抢购_javascript技巧

这篇文章为大家分享了javascript实现倒计时限时抢购,精确到毫秒的倒计时,供大家参考,具体内容如下 一.效果图 下面的图片就是聚划算上面的限时抢的效果 二.实现限时抢的效果需要用到的知识 :Javascript Date()对象 Date()返回当前的日期和事件 getYear()返回年份 获得年最好用 getFullYear()方法来操作(完整格式如2016) getMonth()返回月份值(从0开始,+1) getDay()返回星期几(0-6) getHours()返回小时数(0-23

Android 自定义View之倒计时实例代码

Android 自定义View之倒计时实例代码 需求: 大多数app在注册的时候,都有一个获取验证码的按钮,点击后,访问接口,最终用户会收到短信验证码.为了不多次写这个获取验证码的接口,下面将它自定义成一个view,方便使用. 分析一下,这是一个TextView,点击的时候变色,不能再点击,同时里面的倒计时开始显示.那么就有了下面的代码 代码: /** * 通过selector选择器来改变背景,其中倒计时运行时为android:state_enabled="true", * 不显示倒计

限时抢购秒杀系统架构分析与实战_Android

1 秒杀业务分析 正常电子商务流程 (1)查询商品:(2)创建订单:(3)扣减库存:(4)更新订单:(5)付款:(6)卖家发货 秒杀业务的特性 (1)低廉价格:(2)大幅推广:(3)瞬时售空:(4)一般是定时上架:(5)时间短.瞬时并发量高: 2 秒杀技术挑战 假设某网站秒杀活动只推出一件商品,预计会吸引1万人参加活动,也就说最大并发请求数是10000,秒杀系统需要面对的技术挑战有: 对现有网站业务造成冲击 秒杀活动只是网站营销的一个附加活动,这个活动具有时间短,并发访问量大的特点,如果和网站原

基于PHP实现假装商品限时抢购繁忙的效果_php技巧

最近要做一个项目,有关商品显示抢购的功能.比如我们的网站很带流量,那么成千上万的用户在几秒内同时点你的商品,确实会出现"抢购人数过多,会提示,系统繁忙.     但是呢,大部分网站然而并没有这么牛叉.为了让用户感受到商品很抢手,动不动就提示"系统繁忙"的效果,我们需要做一个程序来"假装很繁忙". (除了淘宝,大家不要以为其他网站真的很繁忙哦,只不过人家是故意让你觉得不抢就买不到,求懂) 本文来设定一个规则,大家可以根据我的思路扩展即可.     1.商品购

jquery简单团购商品倒计时程序代码

 代码如下 复制代码 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>倒计时测试</title> <script src="jquery-1.6.4.min.js" type="text/javascript"></script> <script>  

Android自定义View倒计时圆

本文实例为大家分享了Android自定义View倒计时圆的具体代码,供大家参考,具体内容如下 创建attr <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CountDownView"> <!--颜色--> <attr name="ringColor" format=&q

Android实现时间倒计时功能

本文实例为大家分享了Android实现时间倒计时功能展示的具体代码,供大家参考,具体内容如下 效果展示 MainActivity(主页面代码) public class MainActivity extends Activity { private RelativeLayout countDown; // 倒计时 private TextView daysTv, hoursTv, minutesTv, secondsTv; private long mDay = 10; private long

android 中导入数据库,代码一处错误。请大神帮我看看

问题描述 android 中导入数据库,代码一处错误.请大神帮我看看 解决方案 首先你要确定这个cothinkganbu这个文件在哪儿呢,如果你放在了assets里面那就不是这样调用获取资源了,红线处说明的是找不到这个资源,所以你检查一下看是什么情况. 解决方案二: 清理一下工程看看.... 解决方案三: 请大神帮我看看这段代码