安卓倒计时(android.os.Handler.Handler()

本文详细介绍了如何实现倒计时功能,包括时/分/秒格式的倒计时、分/秒/百分秒格式的倒计时以及简化调用的计时函数。通过集成倒计时逻辑,实现不同格式的倒计时显示,并提供了停止、重新开始等操作,简化了计时过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用一次:updateCountDown()进行倒计时


1、倒计时:时/分/秒


// ===============================================================================
// 限时抢购倒计时
// ===============================================================================

private final int COUNTDOWN = 600;	// 倒计时60 * 10 (秒)
public static long startTime = 0;
private Handler handler = new Handler();

/**
 * 记录当前的时间值
 */
public static void recordCurrentTime()
{
	startTime = new Date().getTime() / 1000;
}

// 显示当前时间值
private void showTime() 
{
	TextView2 mTextView2 = (TextView2) layout.findViewById(R.id.countDown_time);
	if (startTime <= 0) recordCurrentTime();	// 获取初始时间值
	
	// 倒计时
	long countDown = COUNTDOWN - (new Date().getTime() / 1000  - startTime);
	while(countDown < 0)		// 循环倒计时
	{
		countDown += COUNTDOWN;
		startTime += COUNTDOWN;
	}
	
	mTextView2.setText(Tools.secondFormat(countDown));
}

// 更新显示倒计时
private void updateCountDown()
{
	handler.postDelayed(new Runnable()
	{
		@Override
		public void run()
		{
			showTime();
			if(!formExit) updateCountDown();	// 若界面未关闭则继续更新倒计时显示
		}
	}, 1000);
}
	
/**
 *  秒数值格式化为 时/分/秒 字符串
 */
public static String secondFormat(long second)
{
	String str = "";
	long tmp = 0;
	
	// 秒
	tmp = second % 60;
	str = (tmp < 10 ? "0" + tmp : tmp) + str;
	
	// 分
	second /= 60;
	tmp = second % 60;
	str = (tmp < 10 ? "0" + tmp : tmp) + ":" + str;
	
	// 时
	second /= 60;
	tmp = second % 60;
	str = (tmp < 10 ? "0" + tmp : tmp) + ":" + str;
	
	return str;
}
	

2、倒计时 :分/秒/百分秒


// ===============================================================================
// 限时抢购倒计时, 分/秒/百分秒 —— 调用:updateCountDown()
// ===============================================================================

private Handler handler = new Handler();			// 用于更新倒计时

private final int UNIT = 10;						// 最小时间尺寸,10ms
private final int COUNTDOWN = 600 * (1000 / UNIT);	// 倒计时600 (秒)
public static long startTime = 0;

private View[] TimeView = null;
boolean formExit = false;							// 标识界面是否关闭

/**
 * 记录当前的时间值
 */
private void recordCurrentTime()
{
	startTime = new Date().getTime() / UNIT;
}

// 显示当前时间值
private void showTime()
{
	if (startTime <= 0) recordCurrentTime();	// 获取初始时间值
		
	// 倒计时
	long countDown = COUNTDOWN - (new Date().getTime() / UNIT - startTime);
	while (countDown < 0)		// 循环倒计时
	{
		countDown += COUNTDOWN;
		startTime += COUNTDOWN;
	}
	
	if(TimeView == null) TimeView = timeView();
	setView(TimeView, Format(countDown));
}

/** 设置时间值显示, 在V中显示时间time*/
private void setView(View[] V, int[] time)
{
	// 数值0-9对应的图像资源
	final int[] resId = { R.drawable.xuanren_xianshi0, R.drawable.xuanren_xianshi1, R.drawable.xuanren_xianshi2, R.drawable.xuanren_xianshi3,
			R.drawable.xuanren_xianshi4, R.drawable.xuanren_xianshi5, R.drawable.xuanren_xianshi6, R.drawable.xuanren_xianshi7,
			R.drawable.xuanren_xianshi8, R.drawable.xuanren_xianshi9 };
	
	for (int i = 0; i < V.length; i++)
	{
		V[i].setBackgroundResource(resId[time[i]]);
	}
}

/**
 *  获取时间值,对应控件数组
 */
private View[] timeView()
{
	final int[] resId = { R.id.time_num1, R.id.time_num2, R.id.time_num3, R.id.time_num4, R.id.time_num5, R.id.time_num6 };
	
	View[] V = new View[resId.length];
	for(int i=0; i<resId.length; i++)
	{
		V[i] = this.findViewById(resId[i]);
	}
	
	return V;
}

/**
 *  秒数值格式化为 :分/秒/百分秒,数值数组
 */
private int[] Format(long second)
{
	int[] time = new int[6];
	long tmp = 0;
	
	// 秒/100
	tmp = second % (1000 / UNIT);
	time[5] = (int) tmp % 10;
	time[4] = (int) tmp / 10;
	
	second /= (1000 / UNIT);
	
	// 秒
	tmp = second % 60;
	time[3] = (int) tmp % 10;
	time[2] = (int) tmp / 10;
	
	second /= 60;
	
	// 分
	tmp = second % 60;
	time[1] = (int) tmp % 10;
	time[0] = (int) tmp / 10;
	
	return time;
}

/** 更新显示倒计时 */
private void updateCountDown()
{
	handler.postDelayed(new Runnable()
	{
		@Override
		public void run()
		{
			showTime();
			if (!formExit) updateCountDown();	// 若界面未关闭则继续更新倒计时显示
		}
	}, 30);
}

3、计时函数,简化调用

import java.util.Date;

import android.app.Activity;
import android.os.Handler;
import android.view.View;

import com.CL.CrazyRacing.game.R;

/**
 * Timer 计时函数,集成倒计时逻辑
 * -----
 * 2015-11-20 下午2:42:49 
 * wangzhongyuan
 */
public class Timer
{	
	// ===============================================================================
	// 限时抢购倒计时, 分/秒/百分秒 
	// 启动:Timer.start(Timer.getTimeView(root));
	// 停止:Timer.stop();
	// ===============================================================================
	
	private static Handler handler; 							// 用于更新倒计时
	private static View[] TimeView;
	
	private static final int UNIT = 10;							// 最小时间尺寸,10ms
	private static final int COUNTDOWN = 600 * (1000 / UNIT);	// 倒计时600 (秒)
	public static long startTime = 0;
	
	private static boolean running = false; // 标识界面是否关闭
	
	/** 在控件组V上逐位显示倒计时时间值 */
	public static void start(View[] V)
	{
		TimeView = V;
		running = true;
		
		updateCountDown();
	}
	
	/** 停止倒计时 */
	public static void stop()
	{
		running = false;
	}
	
	/** 重新开始倒计时 */
	public static void restart(View[] V)
	{
		startTime = 0;
		start(V);
	}
	
	//--------------------------------------------------------------------------------
	
	/** 更新显示倒计时 */
	private static void updateCountDown()
	{
		if(handler == null)  handler = new Handler();
		
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				showTime();
				if (running) updateCountDown();	// 若处于计时状态则继续更新倒计时显示
				else
				{
					TimeView = null;
					handler = null;
				}
			}
		}, 30);
	}
	
	/**
	 * 记录当前的时间值
	 */
	private static void recordCurrentTime()
	{
		startTime = new Date().getTime() / UNIT;
	}
	
	// 显示当前时间值
	private static void showTime()
	{
		if (startTime <= 0) recordCurrentTime();	// 获取初始时间值
			
		// 倒计时
		long countDown = COUNTDOWN - (new Date().getTime() / UNIT - startTime);
		while (countDown < 0)		// 循环倒计时
		{
			countDown += COUNTDOWN;
			startTime += COUNTDOWN;
		}
		
		setView(TimeView, Format(countDown));
	}
	
	/** 设置时间值显示, 在V中显示时间time*/
	private static void setView(View[] V, int[] time)
	{
		// 数值0-9对应的图像资源
		final int[] resId = { R.drawable.xuanren_xianshi0, R.drawable.xuanren_xianshi1, R.drawable.xuanren_xianshi2, R.drawable.xuanren_xianshi3,
				R.drawable.xuanren_xianshi4, R.drawable.xuanren_xianshi5, R.drawable.xuanren_xianshi6, R.drawable.xuanren_xianshi7,
				R.drawable.xuanren_xianshi8, R.drawable.xuanren_xianshi9 };
		
		for (int i = 0; i < V.length; i++)
		{
			V[i].setBackgroundResource(resId[time[i]]);
		}
	}
	
	/**
	 *  获取,显示时间值的控件数组
	 */
	public static View[] getTimeView(Activity root)
	{
		final int[] resId = { R.id.time_num1, R.id.time_num2, R.id.time_num3, R.id.time_num4, R.id.time_num5, R.id.time_num6 };
		
		View[] V = new View[resId.length];
		for (int i = 0; i < resId.length; i++)
		{
			V[i] = root.findViewById(resId[i]);
		}
		
		return V;
	}
	
	/**
	 *  秒数值格式化为 :分/秒/百分秒,数值数组
	 */
	public static int[] Format(long second)
	{
		int[] time = new int[6];
		long tmp = 0;
		
		// 秒/100
		tmp = second % (1000 / UNIT);
		time[5] = (int) tmp % 10;
		time[4] = (int) tmp / 10;
		
		second /= (1000 / UNIT);
		
		// 秒
		tmp = second % 60;
		time[3] = (int) tmp % 10;
		time[2] = (int) tmp / 10;
		
		second /= 60;
		
		// 分
		tmp = second % 60;
		time[1] = (int) tmp % 10;
		time[0] = (int) tmp / 10;
		
		return time;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值