Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘
下面使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)小鬧鐘,CountDownTimer類其實(shí)很簡(jiǎn)單,一般只需重寫其onFinish和onTick方法就可以實(shí)現(xiàn)倒計(jì)時(shí)小鬧鐘,代碼如下:
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è)置鬧鐘倒計(jì)時(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)建一個(gè)CountDownTimer對(duì)象記錄鬧鐘時(shí)間
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對(duì)象
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;
}
/**
* 停止計(jì)時(shí)
*/
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android獲取監(jiān)聽SD Card狀態(tài)的方法
這篇文章主要介紹了android獲取監(jiān)聽SD Card狀態(tài)的方法,涉及Android實(shí)現(xiàn)SD Card監(jiān)聽的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Android實(shí)現(xiàn)拍照或者選取本地圖片
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照或者選取本地圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Flutter滾動(dòng)組件之SingleChildScrollView使用詳解
這篇文章主要為大家詳細(xì)介紹了Flutter滾動(dòng)組件之SingleChildScrollView使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼
這篇文章主要介紹了 android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
listview 選中高亮顯示實(shí)現(xiàn)方法
當(dāng)點(diǎn)擊左側(cè)ListView后,選中的一行就會(huì)一直呈高亮狀態(tài)顯示,圖中選中行字的顏色顯示為藍(lán)色(注意:是選中行后一直高亮,而不是只是點(diǎn)擊時(shí)高亮),如果再次點(diǎn)擊另外的一行, 則新的那一行就高亮,下面就來實(shí)現(xiàn)這個(gè)高亮效果的顯示2012-11-11
Android實(shí)現(xiàn)游戲中的漸隱和漸現(xiàn)動(dòng)畫效果
本文給大家分享android中實(shí)現(xiàn)游戲中的漸隱漸現(xiàn)的動(dòng)畫效果,在游戲開發(fā)中經(jīng)常會(huì)遇到,對(duì)android漸隱漸現(xiàn)效果感興趣的朋友可以參考下本教程2016-09-09
Android 使用jarsigner給apk簽名的方法詳細(xì)介紹
這篇文章主要介紹了Android 使用jarsigner給apk簽名的方法詳細(xì)介紹的相關(guān)資料,APP 完成需要在一些APP 商店進(jìn)行上傳審核,供用戶下載使用,APP 需要簽名認(rèn)證,需要的朋友可以參考下2016-12-12
Android studio實(shí)現(xiàn)畫板功能
這篇文章主要介紹了Android studio實(shí)現(xiàn)畫板功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android圖片壓縮的原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03
Android實(shí)現(xiàn)讓圖片在屏幕上任意移動(dòng)的方法(拖拽功能)
這篇文章主要介紹了Android實(shí)現(xiàn)讓圖片在屏幕上任意移動(dòng)的方法,實(shí)例分析了Android拖拽功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08

