今天项目用到了抢购时倒计时的功能 ,今天找了好多哥们,也百度了好多,但是没有自己喜欢并且能消化的。好不容易找到了一个能容易看懂的,然而又不符合我想要的需求,所以自己搞一下。不知道是否简单,但是最起码是项目的功能可以实现。(一下午,没白费。开心)
直接上代码好了,我相信都能看懂。我除外。(我搞了一下午)哎。反正现在懂点了。。。
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
希望可以帮到有需求的人。同时以后自己忘记时也能看一眼。
以上就是本文的全部内容,希望对大家的学习有所帮助。