最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)倒計時方法匯總

 更新時間:2017年01月02日 13:28:36   作者:yuminfeng728  
這篇文章主要為大家詳細(xì)總結(jié)了Android實(shí)現(xiàn)倒計時的3種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android開發(fā)中經(jīng)常會有倒計時的功能,下面將總結(jié)出常見的集中實(shí)現(xiàn)方式。

1.直接使用Handler的消息機(jī)制來實(shí)現(xiàn)

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,結(jié)合handler一起實(shí)現(xiàn)倒計時

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(); //取消該任務(wù)
      }
    }
  };

  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秒后,每過一秒鐘執(zhí)行一次該任務(wù)
  }

  @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);
    //第一個參數(shù):倒計時的毫秒數(shù);第二個參數(shù):接收onTick回調(diào)的時間間隔
    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();
    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

登封市| 嘉兴市| 万盛区| 潮安县| 同心县| 竹山县| 瓮安县| 桦南县| 衡阳县| 连山| 栾川县| 青阳县| 黔江区| 清水县| 饶阳县| 周宁县| 永康市| 永春县| 通山县| 抚顺市| 岢岚县| 清河县| 精河县| 陇川县| 察隅县| 华安县| 黑河市| 务川| 德钦县| 资兴市| 石城县| 辉南县| 都兰县| 芦山县| 池州市| 新沂市| 金塔县| 咸丰县| 林甸县| 体育| 永平县|