Android實(shí)現(xiàn)淘寶倒計(jì)時(shí)功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)淘寶倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
一、效果圖(這里為了方便我就沒(méi)弄gif圖了,功能是能動(dòng)的)

二、實(shí)現(xiàn)步驟
1.自定義倒計(jì)時(shí)控件、
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;
/**
* 功 能: 倒計(jì)時(shí)工具類
* 所屬模塊:
* 創(chuàng)建時(shí)間: 2018/11/15
* 功能描述:
*/
@SuppressLint("HandlerLeak")
public class RushBuyCountDownTimerView extends LinearLayout {
// 小時(shí),十位
private TextView tv_hour_decade;
// 小時(shí),個(gè)位
private TextView tv_hour_unit;
// 分鐘,十位
private TextView tv_min_decade;
// 分鐘,個(gè)位
private TextView tv_min_unit;
// 秒,十位
private TextView tv_sec_decade;
// 秒,個(gè)位
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;
// 計(jì)時(shí)器
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: 開始計(jì)時(shí)
* @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: 停止計(jì)時(shí)
* @param
* @return void
* @throws
*/
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
/**
* @throws Exception
*
* @Description: 設(shè)置倒計(jì)時(shí)的時(shí)長(zhǎng)
* @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: 倒計(jì)時(shí)
* @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, "時(shí)間到了",
Toast.LENGTH_SHORT).show();
stop();
}
}
}
}
}
}
}
/**
*
* @Description: 變化十位,并判斷是否需要進(jìn)位
* @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: 變化個(gè)位,并判斷是否需要進(jìn)位
* @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.自定義控件轉(zhuǎn)圓角、
<?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 {
/**
* 功 能: 倒計(jì)時(shí)
* 所屬模塊:
* 創(chuàng)建時(shí)間: 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);
// 設(shè)置時(shí)間(hour,min,sec)
timerView.setTime(10, 0, 10);
// 開始倒計(jì)時(shí)
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實(shí)現(xiàn)淘寶倒計(jì)時(shí)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android獲得設(shè)備狀態(tài)信息、Mac地址、IP地址的方法
- Android之在linux終端執(zhí)行shell腳本直接打印當(dāng)前運(yùn)行app的日志的實(shí)現(xiàn)方法
- Android調(diào)試神器stetho使用詳解和改造
- Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情
- Android Java調(diào)用自己C++類庫(kù)的實(shí)例講解
- Android可自定義神奇動(dòng)效的卡片切換視圖實(shí)例
- Android添加音頻的幾種方法
- Android線程中Handle的使用講解
- Android傳感器SensorEventListener之加速度傳感器
- Android手機(jī)獲取Mac地址的幾種方法
相關(guān)文章
Android?NDK開發(fā)之FFmpeg視頻添加水印
這篇文章主要介紹了在Android?NDK開發(fā)中如何通過(guò)FFmpeg為視頻添加水印,文中的示例代碼講解詳細(xì),對(duì)我們了解Android開發(fā)有一定的幫助,感興趣的可以學(xué)習(xí)一下2021-12-12
Android編程判斷橫屏、豎屏及設(shè)置橫豎屏的方法
這篇文章主要介紹了Android編程判斷橫屏、豎屏及設(shè)置橫豎屏的方法,結(jié)合實(shí)例形式分析了Android針對(duì)橫豎屏的判斷、計(jì)算、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Android Studio使用教程(一):下載與安裝及創(chuàng)建HelloWorld項(xiàng)目
這篇文章主要介紹了Android Studio使用教程(一):下載與安裝及創(chuàng)建HelloWorld項(xiàng)目,本文用詳細(xì)的圖文說(shuō)明講解了Android Studio初步使用,需要的朋友可以參考下2015-05-05
Android自定義StepView仿外賣配送進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義StepView仿外賣配送進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android Studio 配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)步驟
本文主要介紹了Android Studio 配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多功能以及下拉刷新功能的相關(guān)資料,需要的朋友可以參考下2016-01-01
代碼從windows下visual studio到andriod平臺(tái)遷移實(shí)現(xiàn)步驟
這篇文章主要介紹了代碼從windows下visual studio到andriod平臺(tái)遷移的修改記錄的相關(guān)資料,需要的朋友可以參考下2017-01-01

