使用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 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;
}
}
}
Android TextView的子類實現了數字自動增長或減小:TextCounter
http://www.open-open.com/lib/view/open1426473122554.html
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

