CountDownButton:倒计时的Button
来源:互联网 发布:java兼职猪八戒网 编辑:程序博客网 时间:2024/06/05 20:24
使用CountDownTimer写的简洁明了
没有使用CountDownTimer,基于倒计时的TextView而写,没什么特别的,代码:
用法:
没了!
自动获取短信验证码并填充以及倒计时
http://www.devstore.cn/code/info/827.html
用自定义Button实现ToggleButton
点击一个按钮,就会有按下的效果,再点击会弹起,实现一个类似ToggleButton的功能。
Android TextView的子类实现了数字自动增长或减小:TextCounter
http://www.open-open.com/lib/view/open1426473122554.html
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 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");}});
没了!
自动获取短信验证码并填充以及倒计时
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; } } }
Android TextView的子类实现了数字自动增长或减小:TextCounter
http://www.open-open.com/lib/view/open1426473122554.html