国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果

2019-12-12 01:10:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下

直接上代碼,相信都看得懂。

Android已經(jīng)幫封裝好了一個(gè)類,只不過(guò)很多人不知道而已。

代碼:

public class SplashActivity extends BaseAppCompatActivity {    @InjectView(R.id.ivBg)   ImageView ivBg;   @InjectView(R.id.tvSkip)   TextView tvSkip;    int[] imgs = new int[]{       R.mipmap.irving,       R.mipmap.bryant,       R.mipmap.james,       R.mipmap.harden,       R.mipmap.curry};    private CountDownTimer timer;    @Override   protected int getContentViewLayoutID() {     return R.layout.activity_splash;   }    @Override   protected void initViewsAndEvents() {     int index = (int) (Math.random() * imgs.length);      ivBg.setImageResource(imgs[index]);      timer = new CountDownTimer(3500, 1000) {       @Override       public void onTick(long millisUntilFinished) {         tvSkip.setText(String.format(getResources().getString(R.string.skip), (int) (millisUntilFinished / 1000 + 0.1)));       }        @Override       public void onFinish() {         tvSkip.setText(String.format(getResources().getString(R.string.skip), 0));         startActivity(new Intent(mContext, HomeActivity.class));         finish();       }     };     timer.start();   }    @OnClick(R.id.tvSkip)   public void skip() {     if (timer != null)       timer.cancel();      startActivity(new Intent(mContext, HomeActivity.class));     finish();   }    @Override   protected void onDestroy() {     super.onDestroy();      if (timer != null) {       timer.cancel();     }   } } 

調(diào)用的時(shí)候很簡(jiǎn)單:timer.start();

最后說(shuō)明一下:CountDownTimer timer = new CountDownTimer(3500, 1000)中,第一個(gè)參數(shù)表示總時(shí)間,第二個(gè)參數(shù)表示間隔時(shí)間。意思就是每隔一秒會(huì)回調(diào)一次方法onTick,然后10秒之后會(huì)回調(diào)onFinish方法。

布局activity_splash.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent">    <ImageView     android:id="@+id/ivBg"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:scaleType="fitXY" />    <TextView     android:id="@+id/tvSkip"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentRight="true"     android:layout_alignParentTop="true"     android:layout_margin="10dp"     android:background="@drawable/common_button_selector"     android:padding="5dp"     android:text="@string/skip" />  </RelativeLayout> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 庆城县| 屯昌县| 天镇县| 岐山县| 基隆市| 双鸭山市| 南丹县| 栾川县| 湖口县| 历史| 建宁县| 北流市| 腾冲县| 邢台市| 满城县| 墨江| 昭苏县| 辉南县| 多伦县| 大荔县| 慈利县| 合肥市| 定边县| 潮安县| 阿拉善盟| 阜平县| 密山市| 怀集县| 哈尔滨市| 许昌县| 万源市| 东乡族自治县| 安宁市| 铅山县| 英德市| 哈密市| 开化县| 桃园县| 泰顺县| 苏尼特右旗| 石棉县|