Android定时器和倒计时实现淘宝秒杀功能

Android定时器和倒计时实现淘宝秒杀功能,第1张

概述本文实例为大家分享了Android实现淘宝秒杀的具体代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID实现淘宝秒杀的具体代码,供大家参考,具体内容如下

目录结构

效果图:

imageVIEwHolder

public class imageVIEwHolder extends RecyclerVIEw.VIEwHolder {    public ImageVIEw imageVIEw;    public imageVIEwHolder(VIEw itemVIEw) {     super(itemVIEw);     imageVIEw = (ImageVIEw) itemVIEw;   } } 

MyVIEwHolder

public class MyVIEwHolder extends RecyclerVIEw.VIEwHolder {    public TextVIEw textVIEw;    public MyVIEwHolder(VIEw itemVIEw) {     super(itemVIEw);     textVIEw = (TextVIEw) itemVIEw;   } } 

recycleAdapter

package com.nodeprogress.snapupvIEw.SnapUp;  import androID.content.Context; import androID.graphics.color; import androID.support.v7.Widget.RecyclerVIEw; import androID.vIEw.VIEwGroup; import androID.Widget.ImageVIEw; import androID.Widget.TextVIEw;  public class recycleAdapter extends RecyclerVIEw.Adapter {    Context context;    public recycleAdapter(Context context) {     this.context = context;   }     @OverrIDe   public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {      if (vIEwType == 0){       return new imageVIEwHolder(new ImageVIEw(context));     }else {       return new MyVIEwHolder(new TextVIEw(context));     }   }    @OverrIDe   public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder,int position) {     if (getItemVIEwType(position) == 0) {       imageVIEwHolder vIEwHolder = (imageVIEwHolder) holder;       vIEwHolder.imageVIEw.setpadding(150,20,20);       vIEwHolder.imageVIEw.setBackgroundcolor(color.BLUE);     } else {       MyVIEwHolder vIEwHolder = (MyVIEwHolder) holder;       vIEwHolder.textVIEw.setText(" 淘宝 " + position);     }   }    @OverrIDe   public int getItemCount() {     return 21;   }    @OverrIDe   public int getItemVIEwType(int position) {     return (position == 20) ? 0 : 1;   }  } 

MainActivity

package com.nodeprogress.snapupvIEw;  import androID.os.Bundle; import androID.support.v7.app.AppCompatActivity;  import com.nodeprogress.snapupvIEw.SnapUp.recycleAdapter; import com.nodeprogress.snapupvIEw.VIEw.HorizontalRecycleVIEwLoadMore;   public class MainActivity extends AppCompatActivity {    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);      HorizontalRecycleVIEwLoadMore recyclerVIEw = (HorizontalRecycleVIEwLoadMore) findVIEwByID(R.ID.recycle);     recyclerVIEw.setAdapter(new recycleAdapter(MainActivity.this));      SnapUpCountDownTimerVIEw rushBuyCountDownTimerVIEw = (SnapUpCountDownTimerVIEw) findVIEwByID(R.ID.rushBuyCountDownTimerVIEw);     rushBuyCountDownTimerVIEw.setTime(1,55,3);     rushBuyCountDownTimerVIEw.start();   } } 

SnapUpCountDownTimerVIEw

package com.nodeprogress.snapupvIEw;  import androID.annotation.Suppresslint; import androID.content.Context; import androID.content.res.TypedArray; 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;  @Suppresslint("HandlerLeak") public class SnapUpCountDownTimerVIEw 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 SnapUpCountDownTimerVIEw(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.vIEw_countdowntimer,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);      TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.SnapUpCountDownTimerVIEw);     int size = array.getInteger(R.styleable.SnapUpCountDownTimerVIEw_vIEwSize,12);       tv_hour_decade.setTextSize(size);     tv_hour_unit.setTextSize(size);     tv_min_decade.setTextSize(size);     tv_min_unit.setTextSize(size);     tv_sec_decade.setTextSize(size);     tv_sec_unit.setTextSize(size);     ((TextVIEw)vIEw.findVIEwByID(R.ID.colon_minute)).setTextSize(size);     ((TextVIEw)vIEw.findVIEwByID(R.ID.colon_hour)).setTextSize(size);   }     public voID start() {     if (timer == null) {       timer = new Timer();       timer.schedule(new TimerTask() {         @OverrIDe         public voID run() {           handler.sendEmptyMessage(0);         }       },1000);     }   }     public voID stop() {     if (timer != null) {       timer.cancel();       timer = null;     }   }     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("时间格式错误,请检查你的代码");     }      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 + "");   }     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();                 setTime(0,0);//重置为0               }             }           }         }       }     }   }     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;     }   }     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;     }   } } 

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?> <relativeLayout   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:paddingBottom="@dimen/activity_vertical_margin"   androID:paddingleft="@dimen/activity_horizontal_margin"   androID:paddingRight="@dimen/activity_horizontal_margin"   androID:paddingtop="@dimen/activity_vertical_margin"   tools:context="com.nodeprogress.snapupvIEw.MainActivity">     <include layout="@layout/home_snap_up"></include> </relativeLayout> 

home_snap_up.xml

<?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="wrap_content"        xmlns:app="http://schemas.androID.com/apk/res-auto"        androID:orIEntation="vertical"        androID:background="@androID:color/white"        androID:padding="15dp"   >    <linearLayout     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:orIEntation="horizontal"     >      <TextVIEw       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:layout_g@R_404_6610@ty="center_vertical"       androID:text="秒杀"       androID:textcolor="@androID:color/holo_red_light"       androID:textSize="20sp"       />      <com.nodeprogress.snapupvIEw.SnapUpCountDownTimerVIEw       androID:ID="@+ID/rushBuyCountDownTimerVIEw"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:layout_marginleft="20dp"       app:vIEwSize="12"       >     </com.nodeprogress.snapupvIEw.SnapUpCountDownTimerVIEw>       <TextVIEw       androID:layout_wIDth="match_parent"       androID:layout_height="match_parent"       androID:g@R_404_6610@ty="center_vertical|right"       androID:text="更多 >"       androID:textSize="15sp"       />   </linearLayout>    <com.nodeprogress.snapupvIEw.VIEw.HorizontalRecycleVIEwLoadMore     androID:ID="@+ID/recycle"     androID:layout_margintop="10dp"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content">    </com.nodeprogress.snapupvIEw.VIEw.HorizontalRecycleVIEwLoadMore>    </linearLayout> 

vIEw_countdowntimer.xml

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:background="@androID:color/white"        androID:orIEntation="horizontal"   >    <linearLayout     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:background="@drawable/bg_snap_up"     androID:padding="5dp"     >      <TextVIEw       androID:ID="@+ID/tv_hour_decade"       />      <TextVIEw       androID:ID="@+ID/tv_hour_unit"              androID:layout_marginleft="1dp"/>   </linearLayout>     <TextVIEw     androID:ID="@+ID/colon_hour"     androID:layout_wIDth="wrap_content"     androID:layout_height="match_parent"          />    <linearLayout     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:background="@drawable/bg_snap_up"     androID:padding="5dp"     >      <TextVIEw       androID:ID="@+ID/tv_min_decade"       />      <TextVIEw       androID:ID="@+ID/tv_min_unit"              androID:layout_marginleft="1dp"/>   </linearLayout>     <TextVIEw     androID:ID="@+ID/colon_minute"     androID:layout_wIDth="wrap_content"     androID:layout_height="match_parent"          />     <linearLayout     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:background="@drawable/bg_snap_up_red"     androID:padding="5dp"     >      <TextVIEw       androID:ID="@+ID/tv_sec_decade"       />      <TextVIEw       androID:ID="@+ID/tv_sec_unit"              androID:layout_marginleft="1dp"/>   </linearLayout>  </linearLayout> 

代码全部贴出来了。

Android 定时器+倒计时 CountDownTimer实现

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

您可能感兴趣的文章:Android中使用TextView实现高仿京东淘宝各种倒计时效果Android 仿京东秒杀倒计时代码 总结

以上是内存溢出为你收集整理的Android定时器和倒计时实现淘宝秒杀功能全部内容,希望文章能够帮你解决Android定时器和倒计时实现淘宝秒杀功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存