Android开发——倒计时器自定义chronometer

  昨天我们说好了今天做出来一个倒计时器,然后博主不知道倒计时这个逻辑是通过控件实现的还是通过算法实现的就去网上“借”,在网上找了N多实例,然后发现倒计时的还真的是不多。不过也是有的,所以博主找了一个不错的,捋了捋,然后分享给大家。

CountDown.java

package com.example.chronometer;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Chronometer;
/**
 * @author Squid
 *
 */
public class CountDown extends Chronometer{

	private long time;
	private long nextTime;
	private OnTimeCompleteListener clistener;
	private SimpleDateFormat format;
	public CountDown(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		format=new SimpleDateFormat("mm:ss");
		this.setOnChronometerTickListener(listener);
	}
	/*
	 * 
	 * 设置时间格式
	 */
	public void setTimeFormat(String pattern){
		format=new SimpleDateFormat(pattern);
	}
	public void setOnTimeCompleteListener(OnTimeCompleteListener l) {
		clistener=l;
	}
	OnChronometerTickListener listener=new OnChronometerTickListener() {
		
		@Override
		public void onChronometerTick(Chronometer chronometer) {
			// TODO Auto-generated method stub
			if (nextTime<=0) {
				if (nextTime==0) {
					CountDown.this.stop();
					if (null!=clistener) {
						clistener.onTimeComplete();
					}
				}
				nextTime=0;
				updateTimeNext();
				return;
			}
			nextTime--;
			updateTimeNext();
		}
	};
	/*
	 * 初始化时间
	 */
	public void initTime(long _time_s){
		time=nextTime=_time_s;
		updateTimeNext();
	}
	private void updateTimeNext() {
		// TODO Auto-generated method stub
		this.setText(format.format(new Date(nextTime*1000)));
	}
	/*
	 * 重启
	 */
	public void reStart(long _time_s){
		if (_time_s==-1) {
			nextTime=time;
		}else{
			time=nextTime=_time_s;
		}
		this.start();
	}
	public void reStart(){
		reStart(-1);
	}
	/*
	 * 继续计时
	 */
	public void onResume(){
		this.start();
	}
	/*
	 * 暂停计时
	 */
	public void onPause(){
		this.stop();
	}
	interface OnTimeCompleteListener{
		void onTimeComplete();
	}
}
MainActivity.java

package com.example.chronometer;
import com.example.chronometer.CountDown.OnTimeCompleteListener;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
	private CountDown countDown;
	private int initTime=120;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		countDown=(CountDown)findViewById(R.id.cd);
		countDown.initTime(initTime);
		countDown.setOnTimeCompleteListener(new OnTimeCompleteListener(){
			@Override
			public void onTimeComplete() {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "倒计时结束", Toast.LENGTH_LONG).show();
			}
		});
	}
	public void btnClick(View view){
		int id=view.getId();
		switch (id) {
		case R.id.button1:
			countDown.reStart();
			break;
		case R.id.button2:
			countDown.onPause();
			break;
		case R.id.button3:
			countDown.onResume();
			break;
		case R.id.button4:
			countDown.stop();
			countDown.initTime(initTime);
			break;

		default:
			break;
		}
	}
}
activity_main.xml



    
        
        
        
做出来的初始界面就是这样:

Android开发——倒计时器自定义chronometer_第1张图片

我们接下来就开始把今天下午学的东西理出来吧!

你可能感兴趣的:(Android)