读书人

CountDownButton:记时的Button

发布时间: 2012-06-30 17:20:13 作者: rapoo

CountDownButton:倒计时的Button


基于倒计时的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 stubinit();}public CountDownButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinit();}public CountDownButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubinit();}private void init(){text=getText();}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();mTickerStopped = true;}@Overrideprotected 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() {@Overridepublic void onTick() {// TODO Auto-generated method stubLog.i("tag", "onTick");}@Overridepublic void onFinish() {// TODO Auto-generated method stubLog.i("tag", "onFinish");}});

没了!

用自定义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;         }     } }



读书人网 >移动开发

热点推荐