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(); } }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答