android倒計時功能的實現(CountDownTimer)

android倒計時功能的實現(CountDownTimer)

分類: android小例子 2011-11-06 23:18 2550人閱讀 評論(4) 收藏 舉報

在逛論壇的時候,看到一個網友提問,說到了CountDownTimer這個類,從名字上面你們就能夠看出來,記錄下載時間。將後臺線程的建立和Handler隊列封裝成爲了一個方便的類調用。java

查看了一下官方文檔,這個類及其簡單,只有四個方法,上面都涉及到了onTick,onFinsh、cancel和start。其中前面兩個是抽象方法,因此要重寫一下。
下面是官方給的一個小例子:

android

  1. new CountdownTimer(30000, 1000) {
  2. public void onTick(long millisUntilFinished) {
  3. mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
  4. }
  5. public void onFinish() {
  6. mTextField.setText("done!");
  7. }
  8. }.start();
new CountdownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { mTextField.setText("done!"); } }.start();


直接用的那位網友的代碼,本身稍微改動了一下一個簡單的小demo。app

  1. package cn.demo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.content.Intent;
  5. import android.os.CountDownTimer;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. public class NewActivity extends Activity {
  9. private MyCount mc;
  10. private TextView tv;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. // TODO Auto-generated method stub
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. tv = (TextView)findViewById(R.id.show);
  17. mc = new MyCount(30000, 1000);
  18. mc.start();
  19. }//end func
  20. /*定義一個倒計時的內部類*/
  21. class MyCount extends CountDownTimer {
  22. public MyCount(long millisInFuture, long countDownInterval) {
  23. super(millisInFuture, countDownInterval);
  24. }
  25. @Override
  26. public void onFinish() {
  27. tv.setText("finish");
  28. }
  29. @Override
  30. public void onTick(long millisUntilFinished) {
  31. tv.setText("請等待30秒(" + millisUntilFinished / 1000 + ")...");
  32. Toast.makeText(NewActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有顯示時間延遲
  33. }
  34. }
  35. }
package cn.demo; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.os.CountDownTimer; import android.widget.TextView; import android.widget.Toast; public class NewActivity extends Activity { private MyCount mc; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.show); mc = new MyCount(30000, 1000); mc.start(); }//end func /*定義一個倒計時的內部類*/ class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { tv.setText("finish"); } @Override public void onTick(long millisUntilFinished) { tv.setText("請等待30秒(" + millisUntilFinished / 1000 + ")..."); Toast.makeText(NewActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有顯示時間延遲 } } }

主要是重寫onTick和onFinsh這兩個方法,onFinish()中的代碼是計時器結束的時候要作的事情;onTick(Long m)中的代碼是你倒計時開始時要作的事情,參數m是直到完成的時間,構造方法MyCount()中的兩個參數中,前者是倒計的時間數,後者是倒計每秒中間的間隔時間,都是以毫秒爲單位。例如要倒計時30秒,每秒中間間隔時間是1秒,兩個參數能夠這樣寫MyCount(30000,1000)。 將後臺線程的建立和Handler隊列封裝成爲了一個方便的類調用。ide

當你想取消的時候使用mc.cancel()方法就好了。
this

相關文章
相關標籤/搜索