本文将带你了解Android应用开发Andorid实现精确到天时分秒的抢购倒计时,希望本文对大家学Android有所帮助。
今天项目用到了抢购时倒计时的功能 ,今天找了好多哥们,也百度了好多,但是没有自己喜欢并且能消化的。好不容易找到了一个能容易看懂的,然而又不符合我想要的需求,所以自己搞一下。不知道是否简单,但是最起码是项目的功能可以实现。
直接上代码好了
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() {