欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现淘宝倒计时功能

程序员文章站 2022-06-16 14:22:45
本文实例为大家分享了android实现淘宝倒计时的具体代码,供大家参考,具体内容如下 一、效果图(这里为了方便我就没弄gif图了,功能是能动的) 二、实现步骤 1...

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

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

Android实现淘宝倒计时功能

二、实现步骤

1.自定义倒计时控件、

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、

<?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.自定义控件转圆角、

<?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代码

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布局

<?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实现淘宝倒计时

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