CountDownButton:倒计时的Button

使用CountDownTimer写的简洁明了
showToast("验证码已发送,请注意查收");
					
					int timer=60*1000;
					
					new CountDownTimer(timer, 1000) {  
					    public void onTick(long millisUntilFinished) {
					    	btn_code.setEnabled(false);
					    	btn_code.setText("获取验证码("+(millisUntilFinished / 1000)+")");  
					    }  
					    public void onFinish() {
					    	btn_code.setEnabled(true);
					    	btn_code.setText("获取验证码");
					    }  
					 }.start();  





没有使用CountDownTimer,基于倒计时的TextView而写,没什么特别的,代码:
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Button;

public class CountDownButton extends Button {
	private Runnable mTicker;
	private Handler mHandler;

	private boolean mTickerStopped = false;
	
	private OnCountDownListener onCountDownListener;//监听回调
	private int count=10;//倒计时的步数
	private CharSequence text;//原始文字
	public CountDownButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}
	public CountDownButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}
	public CountDownButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init(){
		text=getText();
	}
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mTickerStopped = true;
	}
	@Override
	protected void onAttachedToWindow() {
		mTickerStopped = false;
		super.onAttachedToWindow();
		mHandler = new Handler();

		/**
		 * requests a tick on the next hard-second boundary
		 */
		mTicker = new Runnable() {
			public void run() {
				if (mTickerStopped)
					return;
				if(count<=0){
					if (onCountDownListener != null)
						onCountDownListener.onFinish();
					return;
				}
				count--;
				setText(text+"("+count+")");
				if (onCountDownListener != null)
					onCountDownListener.onTick();
				invalidate();
				long now = SystemClock.uptimeMillis();
				long next = now + (1000 - now % 1000);
				mHandler.postAtTime(mTicker, next);
			}
		};
		mTicker.run();
	}
	
	public interface OnCountDownListener {
		public void onFinish();

		public void onTick();
	}

	public void setOnCountDownListener(OnCountDownListener onCountDownListener) {
		this.onCountDownListener = onCountDownListener;
	}

	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		if(count<0){
			this.count=0;
                        return;
		}
		this.count = count;
	}
}


用法:
CountDownButton btn = (CountDownButton) findViewById(R.id.btn);
		btn.setCount(60);
		btn.setOnCountDownListener(new CountDownButton.OnCountDownListener() {

			@Override
			public void onTick() {
				// TODO Auto-generated method stub
				Log.i("tag", "onTick");
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				Log.i("tag", "onFinish");
			}
		});

没了!

自动获取短信验证码并填充以及倒计时
http://www.devstore.cn/code/info/827.html


用自定义Button实现ToggleButton
点击一个按钮,就会有按下的效果,再点击会弹起,实现一个类似ToggleButton的功能。
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Button;

public class MyTextButton extends Button {

    private boolean checked;

    public MyTextButton(Context context, AttributeSet attrs) { 
        super(context, attrs); 
    }

    @Override 
    public boolean performClick() { 
        this.checked = !this.checked; 
        return super.performClick(); 
    }

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
        if (!checked) { 
            return Button.PRESSED_ENABLED_SELECTED_STATE_SET; 
        } else { 
            return Button.EMPTY_STATE_SET; 
        } 
    } 
}


CountDownButton:倒计时的Button

Android TextView的子类实现了数字自动增长或减小:TextCounter
http://www.open-open.com/lib/view/open1426473122554.html

你可能感兴趣的:(android,handler)