Andorid 實現搶購倒計時能(天時分秒)
阿新 • • 發佈:2019-01-22
今天專案用到了搶購時倒計時的功能 ,今天找了好多哥們,也百度了好多,但是沒有自己喜歡並且能消化的。好不容易找到了一個能容易看懂的,然而又不符合我想要的需求,所以自己搞一下。不知道是否簡單,但是最起碼是專案的功能可以實現。(一下午,沒白費。開心)
直接上程式碼好了,我相信都能看懂。我除外。(我搞了一下午)哎。反正現在懂點了。。。
package com.qust.widght;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.qust.rushbuycountdowntimerview.R;
@SuppressLint("HandlerLeak")
public class RushBuyCountDownTimerView extends LinearLayout {
// 天,十位
private TextView tv_day_decade;
// 天,個位
private TextView tv_day_unit;
// 小時,十位
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 day_decade;
private int day_unit;
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();
};
};
private int day = 0;
private int hour = 0;
private int min = 0;
private int sec = 0;
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.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_day_decade = (TextView) view.findViewById(R.id.tv_day_decade);
tv_day_unit = (TextView) view.findViewById(R.id.tv_day_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;
}
}
// 如果:sum = 12345678
public void addTime(int sum) {
// 求出天數
int day = sum / 60 / 60 / 24;
// int day_time = sum % 24;
Log.e("小時", day + "");
Log.e("小時", sum % 24 + "");
// 求出小時
// int hour = day_time / 60;
// int hour_time = day_time % 60;
//
// Log.e("小時", hour + "");
//
// 先獲取個秒數值
int sec = sum % 60;
// 如果大於60秒,獲取分鐘。(秒數)
int sec_time = sum / 60;
// 再獲取分鐘
int min = sec_time % 60;
// 如果大於60分鐘,獲取小時(分鐘數)。
int min_time = sec_time / 60;
// 獲取小時
int hour = min_time % 24;
// 剩下的自然是天數
day = min_time / 24;
//
// Log.e("分鐘", min + "");
//
// // 求出秒數
// Log.e("秒數", sec + "");
setTime(day, hour, min, sec);
}
/**
* @throws Exception
*
* @Description: 設定倒計時的時長
* @param
* @return void
* @throws
*/
public void setTime(int day, int hour, int min, int sec) {
//這裡的天數不寫也行,我寫365
if (day >= 365 || hour >= 24 || min >= 60 || sec >= 60 || day < 0
|| hour < 0 || min < 0 || sec < 0) {
throw new RuntimeException(
"Time format is error,please check out your code");
}
// day 的十位數
day_decade = day / 10;
// day的個位數,這裡求餘就行
day_unit = day - day_decade * 10;
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;
// 第個time 進行初始化
timeClean();
}
private void timeClean() {
tv_day_decade.setText(day_decade + "");
tv_day_unit.setText(day_unit + "");
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
*/
public Boolean countDown() {
if (isCarry4Unit(tv_sec_unit)) {
if (isCarry4Decade(tv_sec_decade)) {
if (isCarry4Unit(tv_min_unit)) {
if (isCarry4Decade(tv_min_decade)) {
if (isDay4Unit(tv_hour_unit)) {
if (isDay4Decade(tv_hour_decade)) {
if (isDay4Unit(tv_day_unit)) {
if (isDay4Decade(tv_day_decade)) {
Toast.makeText(context, "時間到了",
Toast.LENGTH_SHORT).show();
tv_day_decade.setText("0");
tv_day_unit.setText("0");
tv_hour_decade.setText("0");
tv_hour_unit.setText("0");
tv_min_decade.setText("0");
tv_min_unit.setText("0");
tv_sec_decade.setText("0");
tv_sec_unit.setText("0");
stop();
return false;
}
}
}
}
}
}
}
}
return false;
}
/**
* 進行——時分秒,判斷個位數
*
* @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;
}
}
/**
* 進行——時分秒,判斷個位數
*
* @Description: 變化十位,並判斷是否需要進位
* @param
* @return boolean
* @throws
*/
private boolean isDay4Unit(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 3;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
/**
* 進行——時分秒,判斷個位數
*
* @Description: 變化個位,並判斷是否需要進位
* @param
* @return boolean
* @throws
*/
private boolean isDay4Decade(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 2;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
}
主類進行呼叫
package com.qust.widght;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.qust.rushbuycountdowntimerview.R;
public class MainActivity extends Activity {
private RushBuyCountDownTimerView timerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerView = (RushBuyCountDownTimerView) findViewById(R.id.timerView);
// 設定時間(day,hour,min,sec)
// timerView.setTime(0, 0, 0, 5);
int sum = 60;
// 把秒數傳到倒計時方法中。。
timerView.addTime(sum);
// 開始倒計時
timerView.start();
}
}
佈局檔案
<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:orientation="vertical" >
<com.qust.widght.RushBuyCountDownTimerView
android:id="@+id/timerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.qust.widght.RushBuyCountDownTimerView>
</LinearLayout>
<?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"
android:padding="10dp" >
<TextView
android:id="@+id/tv_day_decade"
style="@style/RushBuyCountDownTimerViewStyle" />
<TextView
android:id="@+id/tv_day_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:text=":"
android:textColor="#4F4242"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_hour_decade"
style="@style/RushBuyCountDownTimerViewStyle" />
<TextView
android:id="@+id/tv_hour_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:text=":"
android:textColor="#4F4242"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_min_decade"
style="@style/RushBuyCountDownTimerViewStyle" />
<TextView
android:id="@+id/tv_min_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:text=":"
android:textColor="#4F4242"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_sec_decade"
style="@style/RushBuyCountDownTimerViewStyle" />
<TextView
android:id="@+id/tv_sec_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp" />
</LinearLayout>
希望可以幫到有需求的人。同時以後自己忘記時也能看一眼