Android仿京东首页秒杀倒计时

 更新时间:2019年01月21日 16:58:32   作者:拉莫帅  
这篇文章主要为大家详细介绍了Android仿京东首页秒杀倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);

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

相关文章

  • Android使用文件进行IPC

    Android使用文件进行IPC

    这篇文章主要为大家详细介绍了Android使用文件进行IPC,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android中SQLite 使用方法详解

    Android中SQLite 使用方法详解

    这篇文章主要介绍了Android中SQLite 使用方法详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Linux系统下安装android sdk的方法步骤

    Linux系统下安装android sdk的方法步骤

    这篇文章主要介绍了Linux系统下安装android sdk的方法步骤,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友可以们下面来一起看看吧。
    2017-03-03
  • Android AlertDialog六种创建方式案例详解

    Android AlertDialog六种创建方式案例详解

    这篇文章主要介绍了Android AlertDialog六种创建方式案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android编程之高效开发App的10个建议

    Android编程之高效开发App的10个建议

    这篇文章主要介绍了Android编程之高效开发App的10个建议,较为详细的分析了Android开发中的常见问题与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android 自定义验证码输入框的实例代码(支持粘贴连续性)

    Android 自定义验证码输入框的实例代码(支持粘贴连续性)

    这篇文章主要介绍了Android 自定义验证码输入框的实例代码(支持粘贴连续性),代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • 深入Android 五大布局对象的应用

    深入Android 五大布局对象的应用

    本篇文章小编为大家介绍,深入Android 五大布局对象的应用。需要的朋友参考下
    2013-04-04
  • 轻松实现Android语音识别功能

    轻松实现Android语音识别功能

    这篇文章主要为初学者介绍了轻松实现Android语音识别功能的代码,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • 一次OOM问题排查过程实战记录

    一次OOM问题排查过程实战记录

    这篇文章主要给大家介绍了一次OOM问题排查过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • Android实现自定义的弹幕效果

    Android实现自定义的弹幕效果

    现在的视频网站基本都带有弹幕效果,满屏幕的文字从右到左飘来飘去。看起来还蛮炫的,这篇文章就是来实现这个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂移移动效果,同时也支持屏幕弹幕最多显示个数的设置。有需要的可以参考借鉴。
    2016-08-08

最新评论