怎么在Android中利用CountDownTimer類實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)鬧鐘?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(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)了近千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎ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è)置鬧鐘倒計(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對象記錄鬧鐘時(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對象 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); } } } }
布局文件:
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。