基本信息
源码名称:android 闹钟 例子源码下载
源码大小:0.15M
文件格式:.zip
开发语言:Java
更新时间:2014-11-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
闹钟
闹钟
package com.example.brewclock;
import android.app.Activity;
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 BrewClockActivity extends Activity implements OnClickListener
{
/** Properties **/
protected Button brewAddTime;
protected Button brewDecreaseTime;
protected Button startBrew;
protected TextView brewCountLabel;
protected TextView brewTimeLabel;
protected int brewTime = 3;
protected CountDownTimer brewCountDownTimer;
protected int brewCount = 0;
protected boolean isBrewing = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Connect interface elements to properties
brewAddTime = (Button) findViewById(R.id.brew_time_up);
brewDecreaseTime = (Button) findViewById(R.id.brew_time_down);
startBrew = (Button) findViewById(R.id.brew_start);
brewCountLabel = (TextView) findViewById(R.id.brew_count_label);
brewTimeLabel = (TextView) findViewById(R.id.brew_time);
// Setup ClickListeners
brewAddTime.setOnClickListener(this);
brewDecreaseTime.setOnClickListener(this);
startBrew.setOnClickListener(this);
// Set the initial brew values
setBrewCount(0);
setBrewTime(3);
}
/** Methods **/
/**
* Set an absolute value for the number of minutes to brew. Has no effect if a brew
* is currently running.
* @param minutes The number of minutes to brew.
*/
public void setBrewTime(int minutes)
{
if(isBrewing)
return;
brewTime = minutes;
if(brewTime < 1)
{
brewTime = 1;
}
brewTimeLabel.setText(String.valueOf(brewTime) "m");
}
/**
* Set the number of brews that have been made, and update the interface.
* @param count The new number of brews
*/
public void setBrewCount(int count)
{
brewCount = count;
brewCountLabel.setText(String.valueOf(brewCount));
}
/**
* Start the brew timer
*/
public void startBrew()
{
// Create a new CountDownTimer to track the brew time
brewCountDownTimer = new CountDownTimer(brewTime * 60 * 1000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
brewTimeLabel.setText(String.valueOf(millisUntilFinished / 1000) "s");
}
@Override
public void onFinish()
{
isBrewing = false;
setBrewCount(brewCount 1);
brewTimeLabel.setText("Brew Up!");
startBrew.setText("Start");
}
};
brewCountDownTimer.start();
startBrew.setText("Stop");
isBrewing = true;
}
/**
* Stop the brew timer
*/
public void stopBrew()
{
if(brewCountDownTimer != null)
{
brewCountDownTimer.cancel();
}
isBrewing = false;
startBrew.setText("Start");
}
/** Interface Implementations **/
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v)
{
if(v == brewAddTime)
{
setBrewTime(brewTime 1);
}
else if(v == brewDecreaseTime)
{
setBrewTime(brewTime -1);
}
else if(v == startBrew)
{
if(isBrewing)
{
stopBrew();
}
else
{
startBrew();
}
}
}
}