新聞中心
這篇文章給大家介紹Android中怎么實(shí)現(xiàn)倒計(jì)時(shí),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
10年的鹽亭網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整鹽亭建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“鹽亭網(wǎng)站設(shè)計(jì)”,“鹽亭網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
CountDownTimer這個(gè)類,實(shí)現(xiàn)了倒計(jì)時(shí)的功能。將后臺線程的創(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();
ackage com.yydcdut.daojishi; import android.os.Bundle; import android.os.CountDownTimer; import android.app.Activity; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private MyCount mc; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);tv = (TextView)findViewById(R.id.show); mc = new MyCount(30000, 1000); mc.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /*定義一個(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("請等待30秒(" + millisUntilFinished / 1000 + ")..."); Toast.makeText(MainActivity.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í)間,都是以毫秒為單位。例如要倒計(jì)時(shí)30秒,每秒中間間隔時(shí)間是1秒,兩個(gè)參數(shù)可以這樣寫MyCount(30000,1000)。 將后臺線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的類調(diào)用。
關(guān)于Android中怎么實(shí)現(xiàn)倒計(jì)時(shí)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:Android中怎么實(shí)現(xiàn)倒計(jì)時(shí)
文章URL:http://fisionsoft.com.cn/article/pohded.html