欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年02月08日 10:52:52   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

Android已經(jīng)幫封裝好了一個(gè)類(lèi),只不過(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android View 事件防抖的兩種方案

    Android View 事件防抖的兩種方案

    這篇文章主要介紹了Android View 事件防抖的兩種方案,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android頂欄定時(shí)推送消息

    Android頂欄定時(shí)推送消息

    在用安卓設(shè)備時(shí),經(jīng)常會(huì)應(yīng)用到彈出推送消息。接下來(lái)通過(guò)本文給大家介紹Android頂欄定時(shí)推送消息,感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04
  • Android開(kāi)發(fā)Jetpack組件LiveData使用講解

    Android開(kāi)發(fā)Jetpack組件LiveData使用講解

    LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話(huà)說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)
    2022-08-08
  • Android Support Palette使用詳解

    Android Support Palette使用詳解

    這篇文章主要介紹了Android Support Palette使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 詳解Android 教你打造高效的圖片加載框架

    詳解Android 教你打造高效的圖片加載框架

    本篇文章主要介紹了詳解Android 教你打造高效的圖片加載框架,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • Android編程中Perferences的用法實(shí)例分析

    Android編程中Perferences的用法實(shí)例分析

    這篇文章主要介紹了Android編程中Perferences的用法,以實(shí)例形式較為詳細(xì)的分析了配置文件preferences.xml的功能、定義及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android  xml解析實(shí)例詳解

    Android xml解析實(shí)例詳解

    這篇文章主要介紹了Android xml解析實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android自動(dòng)化測(cè)試處理各種彈窗的操作方法

    Android自動(dòng)化測(cè)試處理各種彈窗的操作方法

    這篇文章主要介紹了Android自動(dòng)化測(cè)試中如何處理各種彈窗,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • Android自定義照相機(jī)詳解

    Android自定義照相機(jī)詳解

    幾乎每個(gè)APP都會(huì)用的相機(jī)功能,下面小編把內(nèi)容整理分享到腳本之家平臺(tái),供大家參考
    2016-04-04
  • Android性能優(yōu)化getResources()與Binder導(dǎo)致界面卡頓優(yōu)化

    Android性能優(yōu)化getResources()與Binder導(dǎo)致界面卡頓優(yōu)化

    這篇文章主要為大家介紹了Android性能優(yōu)化getResources()與Binder導(dǎo)致界面卡頓優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論