亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能

 更新時(shí)間:2015年12月14日 11:10:54   作者:傲慢的上校  
這篇文章主要介紹了Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,簡單分析了基于CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)功能的技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能的方法。分享給大家供大家參考,具體如下:

在逛論壇的時(shí)候,看到一個(gè)網(wǎng)友提問,說到了CountDownTimer這個(gè)類,從名字上面大家就可以看出來,記錄下載時(shí)間。將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成一個(gè)方便的類調(diào)用。

查看了一下官方文檔,這個(gè)類及其簡單,只有四個(gè)方法,上面都涉及到了onTick,onFinsh、cancel和start。其中前面兩個(gè)是抽象方法,所以要重寫一下。

下面是官方給的一個(gè)小例子:

new CountdownTimer(30000, 1000) {
  public void onTick(long millisUntilFinished) {
    mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
  }
  public void onFinish() {
    mTextField.setText("done!");
  }
}.start();

直接用的那位網(wǎng)友的代碼,自己稍微改動(dòng)了一下,一個(gè)簡單的小demo。

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
  /*定義一個(gè)倒計(jì)時(shí)的內(nèi)部類*/
  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("請(qǐng)等待30秒(" + millisUntilFinished / 1000 + ")...");
      Toast.makeText(NewActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有顯示時(shí)間延遲
    }
  }
}

主要是重寫onTick和onFinsh這兩個(gè)方法,onFinish()中的代碼是計(jì)時(shí)器結(jié)束的時(shí)候要做的事情;onTick(Long m)中的代碼是你倒計(jì)時(shí)開始時(shí)要做的事情,參數(shù)m是直到完成的時(shí)間,構(gòu)造方法MyCount()中的兩個(gè)參數(shù)中,前者是倒計(jì)的時(shí)間數(shù),后者是倒計(jì)時(shí)onTick事件響應(yīng)的間隔時(shí)間,都是以毫秒為單位。例如要倒計(jì)時(shí)30秒,每秒中間間隔時(shí)間是1秒,兩個(gè)參數(shù)可以這樣MyCount(30000,1000)。 將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的類調(diào)用。

當(dāng)你想取消的時(shí)候使用mc.cancel()方法就行了。

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論