編輯:關於Android編程
本文實例總結了Android實現計時與倒計時的常用方法。分享給大家供大家參考,具體如下:
方法一
Timer與TimerTask(Java實現)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { recLen--; txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } }); } }; }
方法二
TimerTask與Handler(不用Timer的改進型)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } final Handler handler = new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what) { case 1: txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } } }; TimerTask task = new TimerTask() { @Override public void run() { recLen--; Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; }
方法三
Handler與Message(不用TimerTask)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); Message message = handler.obtainMessage(1); // Message handler.sendMessageDelayed(message, 1000); } final Handler handler = new Handler(){ public void handleMessage(Message msg){ // handle message switch (msg.what) { case 1: recLen--; txtView.setText("" + recLen); if(recLen > 0){ Message message = handler.obtainMessage(1); handler.sendMessageDelayed(message, 1000); // send message }else{ txtView.setVisibility(View.GONE); } } super.handleMessage(msg); } }; }
方法四
Handler與Thread(不占用UI線程)
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); new Thread(new MyThread()).start(); // start thread } final Handler handler = new Handler(){ // handle public void handleMessage(Message msg){ switch (msg.what) { case 1: recLen++; txtView.setText("" + recLen); } super.handleMessage(msg); } }; public class MyThread implements Runnable{ // thread @Override public void run(){ while(true){ try{ Thread.sleep(1000); // sleep 1000ms Message message = new Message(); message.what = 1; handler.sendMessage(message); }catch (Exception e) { } } } } }
方法五
Handler與Runnable(最簡單型)
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); handler.postDelayed(runnable, 1000); } Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { recLen++; txtView.setText("" + recLen); handler.postDelayed(this, 1000); } }; }
計時與倒計時
方法1,方法2和方法3,都是倒計時
方法4,方法5,都是計時
計時和倒計時,都可使用上述方法實現(代碼稍加改動)
UI線程比較
方法1,方法2和方法3,都是在UI線程實現的計時;
方法4和方法5,是另開Runnable線程實現計時
實現方式比較
方法1,采用的是Java實現,即Timer和TimerTask方式;
其它四種方法,都采用了Handler消息處理
推薦使用
如果對UI線程交互要求不很高,可以選擇方法2和方法3
如果考慮到UI線程阻塞,嚴重影響到用戶體驗,推薦使用方法4,另起線程單獨用於計時和其它的邏輯處理
方法5,綜合了前幾種方法的優點,是最簡的
希望本文所述對大家Android程序設計有所幫助。
(一)概述手勢是:連續觸碰的行為,比如左右上下滑動屏幕,又或者畫一些不規則的幾何圖形! Android對上述兩種手勢行為都提供了支持:Android提供手勢檢測,並為手勢
Fragments 誕生初衷自從Android 3.0中引入fragments 的概念,根據詞海的翻譯可以譯為:碎片、片段。其上的是為了解決不同屏幕分辯率的動態和靈活UI
在4.5.6節介紹過一個<include>標簽,該標簽可以在布局文件中引用另外一個布局文件,並可以覆蓋被引用布局文件根節點所有與布局相關的屬性,也就是以and
這幾天被AsyncTask虐得不行,在此總結下 首先: AsyncTask的參數介紹 在開發Android移動客戶端的時候往往要使用多線程來進行操作,我們通常會