Android 編程下的計(jì)時(shí)器代碼

同樣,為了防止用戶惡意的頻繁發(fā)送激活碼,應(yīng)用中需要對用戶發(fā)送激活碼的時(shí)間間隔進(jìn)行限制,這時(shí)就需要用到倒計(jì)時(shí)器了,大概流程是這樣的:頁面初始化的時(shí)候,按鈕為可點(diǎn)擊狀態(tài),用戶在點(diǎn)擊“發(fā)送激活碼”后按鈕變?yōu)椴豢牲c(diǎn)擊狀態(tài),同時(shí)按鈕上的文字變?yōu)榈褂?jì)時(shí)狀態(tài),倒計(jì)時(shí)結(jié)束后,按鈕變?yōu)榭牲c(diǎn)擊狀態(tài),文字變?yōu)椤鞍l(fā)送激活碼”。具體邏輯看下面的代碼:
package cn.sunzn.countdown;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private TimeCount timeCount;
private Button btn_reget_captcha;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
initView();
initData();
}
private void initData() {
timeCount = new TimeCount(60000, 1000);
}
private void initView() {
btn_reget_captcha = (Button) findViewById(R.id.btn_reget_captcha);
btn_reget_captcha.setOnClickListener(this);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_reget_captcha:
if (btn_reget_captcha.isClickable()) {
// TODO run your logic that you want to do
timeCount.start();
}
break;
default:
break;
}
}
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
btn_reget_captcha.setText("發(fā)送激活碼");
btn_reget_captcha.setClickable(true);
}
public void onTick(long millisUntilFinished) {
btn_reget_captcha.setClickable(false);
btn_reget_captcha.setText("在" + millisUntilFinished / 1000 + "秒后點(diǎn)擊重發(fā)激活碼");
}
}
}
最后,附上工程代碼:CountDown
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android編程之簡單計(jì)時(shí)器實(shí)現(xiàn)方法
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android實(shí)現(xiàn)計(jì)時(shí)器功能
相關(guān)文章
Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn)
這篇文章主要介紹了Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android編程實(shí)現(xiàn)ImageView圖片拋物線動畫效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)ImageView圖片拋物線動畫效果的方法,實(shí)例分析了Android實(shí)現(xiàn)拋物線運(yùn)動的算法原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈2013-06-06
Android TextView實(shí)現(xiàn)詞組高亮的示例代碼
本篇文章主要介紹了Android TextView實(shí)現(xiàn)詞組高亮的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android自定義FloatingText仿點(diǎn)贊+1特效
這篇文章主要為大家詳細(xì)介紹了Android自定義FloatingText仿點(diǎn)贊+1特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
UiOS開發(fā)中ITextView回收或關(guān)閉鍵盤使用方法總結(jié)
iOS開發(fā)中,發(fā)現(xiàn)UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那么要實(shí)現(xiàn)UITextView關(guān)閉鍵盤,必須使用其他的方法,下面是可以使用的幾種方法,需要的朋友參考下吧2016-11-11
Android入門之TableLayout應(yīng)用解析(二)
這篇文章主要介紹了Android入門之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08
Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView(附源碼)
這篇文章主要介紹了Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView的方法,結(jié)合實(shí)例形式分析了ListView動態(tài)更新的常用技巧,并附上完整實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-12-12

