android開發中倒計時功能的實現

來源:互聯網
上載者:User

Android開發中用到了倒計時,查了一些資料,後來看了原始碼,寫了出來,與大家分享,不當之處歡迎指正。

自己要寫個類來繼承CountDownTimer,如下面代碼所示,重寫兩個方法,就是在兩個方法中寫上你要乾的事情。onFinish()中的代碼是結束的時候要做的事情;onTick(Long m)中的代碼是你倒計時開始時要做的事情,參數m是直到完成的時間,實際應用中這個參數必須有但是感覺不到對應用的影響。構造方法MyCount()中的兩個參數中,前者是倒計的時間數,後者是倒計每秒中間的間隔時間,都是以毫秒為單位。例如要倒計時30秒,每秒中間間隔時間是1秒,兩個參數可以這樣寫MyCount(30000,1000)。

 

public class MyCount extends CountDownTimer {</p><p>TextView tt = (TextView) findViewById(R.id.position);</p><p>public MyCount(long millisInFuture, long countDownInterval) {<br />super(millisInFuture, countDownInterval);<br />}</p><p>@Override<br />public void onFinish() {<br />tt.setText("");</p><p>}</p><p>@Override<br />public void onTick(long millisUntilFinished) {<br />tt.setText("請等待60秒(" + millisUntilFinished / 1000 + ")...");<br />}</p><p>}

 

在你的代碼中執行個體化一個MyCount:

MyCount myCount = new MyCount(30000,1000);

 

開始倒計時:

myCount.start();

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.