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

同樣,為了防止用戶(hù)惡意的頻繁發(fā)送激活碼,應(yīng)用中需要對(duì)用戶(hù)發(fā)送激活碼的時(shí)間間隔進(jìn)行限制,這時(shí)就需要用到倒計(jì)時(shí)器了,大概流程是這樣的:頁(yè)面初始化的時(shí)候,按鈕為可點(diǎn)擊狀態(tài),用戶(hù)在點(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開(kāi)發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法
- Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android實(shí)現(xiàn)計(jì)時(shí)器功能
相關(guān)文章
Android 運(yùn)用@JvmName解決函數(shù)簽名沖突問(wèn)題詳解
JvmName注解是Kotlin提供的一個(gè)可以變更編譯器輸出的注解,這里簡(jiǎn)單的介紹一下其使用規(guī)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-07-07
使用Timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式
這篇文章主要介紹了使用Timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式,在使用WebView加載網(wǎng)頁(yè)時(shí)有時(shí)候網(wǎng)速等原因加載比較慢時(shí),影響用戶(hù)的體驗(yàn)度,今天小編給大家分享使用timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式,需要的的朋友參考下吧2017-01-01
android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android實(shí)現(xiàn)監(jiān)聽(tīng)音量的變化
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)監(jiān)聽(tīng)音量的變化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
當(dāng)Button有多個(gè)或者Button的使用次數(shù)很多時(shí),我們需要采用綁定監(jiān)聽(tīng)器的做法,其實(shí),綁定監(jiān)聽(tīng)器也有幾種方法,不過(guò),我在這里就不一一列舉了,畢竟那些方法在實(shí)際的應(yīng)用中也不常見(jiàn)2013-10-10
Android模仿美團(tuán)頂部的滑動(dòng)菜單實(shí)例代碼
最近在工作遇到一個(gè)需要,要做一個(gè)滑動(dòng)菜單,實(shí)現(xiàn)的效果類(lèi)似美團(tuán)頂部的滑動(dòng)菜單,所以下面這篇文章主要給大家介紹了關(guān)于Android如何模仿美團(tuán)頂部滑動(dòng)菜單的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08

