最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用CountDownTimer類實現(xiàn)倒計時鬧鐘

 更新時間:2018年01月25日 10:23:43   作者:IT-馮健  
這篇文章主要為大家詳細介紹了Android使用CountDownTimer類實現(xiàn)倒計時鬧鐘,具有一定的參考價值,感興趣的小伙伴們可以參考一下

下面使用CountDownTimer類實現(xiàn)倒計時小鬧鐘,CountDownTimer類其實很簡單,一般只需重寫其onFinish和onTick方法就可以實現(xiàn)倒計時小鬧鐘,代碼如下:

MainActivity:

package com.home.brewclock; 
 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
 
public class MainActivity extends Activity implements OnClickListener { 
  private Button addTimeBtn; 
  private Button decreaseTimeBtn; 
  private Button startBtn; 
  private Button closeMusicBtn; 
  private TextView timeText; 
 
  private int brewTime = 3; 
  private CountDownTimer countDownTimer; 
  private boolean isBrewing = false; 
  private MediaPlayer alarmMusic; 
 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
 
    addTimeBtn = (Button) findViewById(R.id.main_btn_up); 
    decreaseTimeBtn = (Button) findViewById(R.id.main_btn_down); 
    startBtn = (Button) findViewById(R.id.main_start); 
    closeMusicBtn = (Button) findViewById(R.id.main_btn_close_music); 
    timeText = (TextView) findViewById(R.id.main_tv); 
    addTimeBtn.setOnClickListener(this); 
    decreaseTimeBtn.setOnClickListener(this); 
    startBtn.setOnClickListener(this); 
    closeMusicBtn.setOnClickListener(this); 
    setBrewTime(3); 
  } 
 
  /** 
   * 設(shè)置鬧鐘倒計時初始值 
   * 
   * @param minutes 
   */ 
  public void setBrewTime(int minutes) { 
    if (isBrewing) 
      return; 
    brewTime = minutes; 
 
    if (brewTime < 1) { 
      brewTime = 1; 
    } 
    timeText.setText(String.valueOf(brewTime) + "m"); 
  } 
 
  /** 
   * 開啟鬧鐘 
   */ 
  public void startBrew() { 
    // 創(chuàng)建一個CountDownTimer對象記錄鬧鐘時間 
    countDownTimer = new CountDownTimer(brewTime * 60 * 1000, 1000) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
        timeText.setText(String.valueOf(millisUntilFinished / 1000) 
            + "s"); 
      } 
 
      @Override 
      public void onFinish() { 
        isBrewing = false; 
        timeText.setText(brewTime + "m"); 
        startBtn.setText("Start"); 
        // 加載指定音樂,并為之創(chuàng)建MediaPlayer對象 
        alarmMusic = MediaPlayer.create(MainActivity.this, R.raw.music); 
        // 設(shè)置為循環(huán)播放 
        alarmMusic.setLooping(true); 
        // 播放音樂 
        alarmMusic.start(); 
        closeMusicBtn.setVisibility(0); 
      } 
    }; 
    countDownTimer.start(); 
    startBtn.setText("Stop"); 
    isBrewing = true; 
  } 
 
  /** 
   * 停止計時 
   */ 
  public void stopBrew() { 
    if (countDownTimer != null) { 
      countDownTimer.cancel(); 
    } 
    isBrewing = false; 
    startBtn.setText("Start"); 
  } 
 
  @Override 
  public void onClick(View v) { 
    if (v == addTimeBtn) { 
      setBrewTime(brewTime + 1); 
    } else if (v == decreaseTimeBtn) { 
      setBrewTime(brewTime - 1); 
    } else if (v == startBtn) { 
      if (isBrewing) { 
        stopBrew(); 
      } else { 
        startBrew(); 
      } 
    } else if (v == closeMusicBtn) { 
      if (alarmMusic != null) { 
        alarmMusic.stop(); 
        closeMusicBtn.setVisibility(8); 
      } 
    } 
  } 
} 

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
 
  <Button 
    android:id="@+id/main_btn_close_music" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="關(guān)閉音樂" 
    android:visibility="gone" /> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center" 
    android:orientation="horizontal" > 
 
    <Button 
      android:id="@+id/main_btn_down" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="-" 
      android:textSize="40dp" /> 
 
    <TextView 
      android:id="@+id/main_tv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="0:00" 
      android:textSize="40dp" /> 
 
    <Button 
      android:id="@+id/main_btn_up" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="+" 
      android:textSize="40dp" /> 
  </LinearLayout> 
 
  <Button 
    android:id="@+id/main_start" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="Start" /> 
 
</RelativeLayout> 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android獲取監(jiān)聽SD Card狀態(tài)的方法

    android獲取監(jiān)聽SD Card狀態(tài)的方法

    這篇文章主要介紹了android獲取監(jiān)聽SD Card狀態(tài)的方法,涉及Android實現(xiàn)SD Card監(jiān)聽的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Android實現(xiàn)拍照或者選取本地圖片

    Android實現(xiàn)拍照或者選取本地圖片

    這篇文章主要為大家詳細介紹了Android實現(xiàn)拍照或者選取本地圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Flutter滾動組件之SingleChildScrollView使用詳解

    Flutter滾動組件之SingleChildScrollView使用詳解

    這篇文章主要為大家詳細介紹了Flutter滾動組件之SingleChildScrollView使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼

    Android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼

    這篇文章主要介紹了 android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • listview 選中高亮顯示實現(xiàn)方法

    listview 選中高亮顯示實現(xiàn)方法

    當點擊左側(cè)ListView后,選中的一行就會一直呈高亮狀態(tài)顯示,圖中選中行字的顏色顯示為藍色(注意:是選中行后一直高亮,而不是只是點擊時高亮),如果再次點擊另外的一行, 則新的那一行就高亮,下面就來實現(xiàn)這個高亮效果的顯示
    2012-11-11
  • Android實現(xiàn)游戲中的漸隱和漸現(xiàn)動畫效果

    Android實現(xiàn)游戲中的漸隱和漸現(xiàn)動畫效果

    本文給大家分享android中實現(xiàn)游戲中的漸隱漸現(xiàn)的動畫效果,在游戲開發(fā)中經(jīng)常會遇到,對android漸隱漸現(xiàn)效果感興趣的朋友可以參考下本教程
    2016-09-09
  • Android 使用jarsigner給apk簽名的方法詳細介紹

    Android 使用jarsigner給apk簽名的方法詳細介紹

    這篇文章主要介紹了Android 使用jarsigner給apk簽名的方法詳細介紹的相關(guān)資料,APP 完成需要在一些APP 商店進行上傳審核,供用戶下載使用,APP 需要簽名認證,需要的朋友可以參考下
    2016-12-12
  • Android studio實現(xiàn)畫板功能

    Android studio實現(xiàn)畫板功能

    這篇文章主要介紹了Android studio實現(xiàn)畫板功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Android開發(fā)之圖片壓縮實現(xiàn)方法分析

    Android開發(fā)之圖片壓縮實現(xiàn)方法分析

    這篇文章主要介紹了Android開發(fā)之圖片壓縮實現(xiàn)方法,結(jié)合實例形式分析了Android圖片壓縮的原理、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-03-03
  • Android實現(xiàn)讓圖片在屏幕上任意移動的方法(拖拽功能)

    Android實現(xiàn)讓圖片在屏幕上任意移動的方法(拖拽功能)

    這篇文章主要介紹了Android實現(xiàn)讓圖片在屏幕上任意移動的方法,實例分析了Android拖拽功能的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08

最新評論

辽宁省| 盐城市| 兴和县| 志丹县| 涿州市| 江川县| 盈江县| 兰考县| 哈巴河县| 靖边县| 通许县| 屏边| 武功县| 崇州市| 兰溪市| 黄冈市| 东阿县| 都江堰市| 龙江县| 静宁县| 竹山县| 贵港市| 青神县| 安化县| 通海县| 慈利县| 凤阳县| 江陵县| 宾川县| 呼和浩特市| 尉犁县| 江油市| 四子王旗| 西贡区| 宿松县| 大英县| 漳浦县| 峡江县| 东乌珠穆沁旗| 宣化县| 玛纳斯县|