CountDownButton:倒计时的Button

系统 1447 0
使用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

CountDownButton:倒计时的Button


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论