怎么在Android中利用CountDownTimer實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能

這篇文章主要介紹了怎么在Android中利用CountDownTimer實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能,此處通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)祁縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

代碼如下:

//假設(shè) 這是引導(dǎo)頁面最后一個(gè)界面
public class MainActivity extends Activity {
 private TextView count_time;
 private MyCountDownTimer myCountDownTimer;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 count_time= (TextView) findViewById(R.id.count_time);
 //new對(duì)象,傳入要停留的時(shí)間
 myCountDownTimer=new MyCountDownTimer(5100,1000);
 //開始倒計(jì)時(shí)
 myCountDownTimer.start();
 }
 //自定義一個(gè)類繼承自CountDownTimer,實(shí)現(xiàn)多少秒后的邏輯
 class MyCountDownTimer extends CountDownTimer{
 /**
  *
  * @param millisInFuture
  * 表示以毫秒為單位 倒計(jì)時(shí)的總數(shù)
  *
  * 例如 millisInFuture=1000 表示1秒
  *
  * @param countDownInterval
  * 表示 間隔 多少微秒 調(diào)用一次 onTick 方法
  *
  * 例如: countDownInterval =1000 ; 表示每1000毫秒調(diào)用一次onTick()
  *
  */
 public MyCountDownTimer(long millisInFuture, long countDownInterval) {
  super(millisInFuture, countDownInterval);
 }

 @Override
 public void onTick(long millisUntilFinished) {
  count_time.setText(millisUntilFinished/1000+"s");
 }

 @Override
 public void onFinish() {
  //停止倒計(jì)時(shí)
  myCountDownTimer.cancel();
  //實(shí)現(xiàn)頁面跳轉(zhuǎn)
  startActivity(new Intent(MainActivity.this,SecondActivity.class));
 }
 }
}
//引導(dǎo)頁最后一個(gè)界面的.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >

 <TextView
 android:id="@+id/count_time"
 android:background="@drawable/bg"
 android:textColor="@android:color/holo_blue_bright"
 android:layout_width="60dp"
 android:layout_height="30dp"
 android:layout_centerInParent="true"/>

</RelativeLayout>
//這是跳轉(zhuǎn)后的界面
public class SecondActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 }
}
//引導(dǎo)頁跳轉(zhuǎn)后界面的.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >

 <TextView
 android:textColor="@android:color/holo_blue_bright"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="這是跳轉(zhuǎn)后的頁面"
 android:textSize="30sp"
 android:id="@+id/time" />

</RelativeLayout>

到此這篇關(guān)于怎么在Android中利用CountDownTimer實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能的文章就介紹到這了,更多相關(guān)怎么在Android中利用CountDownTimer實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!

新聞標(biāo)題:怎么在Android中利用CountDownTimer實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能
網(wǎng)站鏈接:http://muchs.cn/article20/ieppjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站標(biāo)簽優(yōu)化、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管