偷懒代码块之数字时钟效果

贴代码备忘~

public class MainActivity extends Activity {

	private TextView tv;
	private static long start,now;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tv = (TextView) findViewById(R.id.tvTime);
		start = System.currentTimeMillis();
		handle.post(runnable);
	}

	public String showTimeCount(long time) {
		if(time >= 360000000){
			return "00:00:00";
		}
		String timeCount = "";
		long hourc = time/3600000;
		String hour = "0" + hourc;
		hour = hour.substring(hour.length()-2, hour.length());

		long minuec = (time-hourc*3600000)/(60000);
		String minue = "0" + minuec;
		minue = minue.substring(minue.length()-2, minue.length());

		long secc = (time-hourc*3600000-minuec*60000)/1000;
		String sec = "0" + secc;
		sec = sec.substring(sec.length()-2, sec.length());
		timeCount = hour + ":" + minue + ":" + sec;
		return timeCount;
	}

	Handler handle = new Handler();
	Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			handle.postDelayed(this, 500);
			now = System.currentTimeMillis();
			long time = now - start;
			tv.setText(showTimeCount(time));
			Log.i("0630","handle -- running");
		}
	};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值