国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現倒計時方法匯總

2019-12-12 04:09:40
字體:
來源:轉載
供稿:網友

Android開發中經常會有倒計時的功能,下面將總結出常見的集中實現方式。

1.直接使用Handler的消息機制來實現

xml布局中文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="horizontal" >  <Button    android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="clickButton"    android:text="開始計時" /></LinearLayout>

java代碼如下:

import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity{  private Button button;  private int count = 60;  private int COUNT_TIME = 0;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.first_activity);    button = (Button) findViewById(R.id.button);  }  private Handler handler = new Handler(){    @Override    public void handleMessage(Message msg) {      if(count <= 0){        count = 60;        button.setText("重新計時");        button.setClickable(true);        return;      }      count--;      button.setText(""+count);      sendEmptyMessageDelayed(COUNT_TIME,1000);    }  };  public void clickButton(View view){    handler.sendEmptyMessage(COUNT_TIME);    button.setClickable(false);  }}

2.使用Timer和TimerTask,結合handler一起實現倒計時

import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity{  private Button button;  private int count = 30;  private int COUNT_TIME = 0;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.first_activity);    button = (Button) findViewById(R.id.button);  }  private Handler handler = new Handler(){    @Override    public void handleMessage(Message msg) {      button.setText(""+count);      if(count <= 0){        count = 30;        button.setClickable(true);        button.setText("重新計時");        timerTask.cancel(); //取消該任務      }    }  };  private Timer timer = new Timer();  private TimerTask timerTask;  public void clickButton(View view){    button.setClickable(false);    timerTask = new TimerTask() {      @Override      public void run() {        count--;        handler.sendEmptyMessage(COUNT_TIME);      }    };    timer.schedule(timerTask,0,1000); //0秒后,每過一秒鐘執行一次該任務  }  @Override  protected void onDestroy() {    super.onDestroy();    //釋放資源    if(timerTask != null){      timerTask.cancel();      timerTask = null;    }    if(timer != null){      timer.cancel();      timer = null;    }  }}

3.使用android自帶的原生倒計時類 CountDownTimer

import android.app.Activity;import android.os.Bundle;import android.os.CountDownTimer;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity{  private Button button;  private CountDownTimer timer;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.first_activity);    button = (Button) findViewById(R.id.button);  }  public void clickButton(View view){    button.setClickable(false);    //第一個參數:倒計時的毫秒數;第二個參數:接收onTick回調的時間間隔    timer = new CountDownTimer(30000, 10) {       public void onTick(long millisUntilFinished) {         button.setText(millisUntilFinished / 1000 + "秒");       }       public void onFinish() {         button.setText("重新計時");         button.setClickable(true);       }     };    timer.start();  }  @Override  protected void onDestroy() {    super.onDestroy();    if(timer != null){      timer.cancel();    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃江县| 万盛区| 文水县| 织金县| 沁阳市| 灵璧县| 莱西市| 平塘县| 云和县| 界首市| 广水市| 昭苏县| 连江县| 都昌县| 巴中市| 兴山县| 响水县| 辛集市| 罗平县| 乐平市| 普兰县| 衡南县| 高雄市| 高邮市| 吉安县| 松滋市| 扶沟县| 广南县| 赫章县| 甘洛县| 汶川县| 仙游县| 伊通| 西林县| 鄢陵县| 洛隆县| 旅游| 曲靖市| 延长县| 西昌市| 监利县|