接上篇Android 开发第五弹:简易时钟(闹钟) ,这次是一个时钟类应用,目前依旧是主要的功能,长得还是很挫。当然了,核心功能是有的……
时钟
先把简单的时钟给列出来吧,这里都写的很简单,即便要用世界各个城市的话,也只是相应的加上或减去几个小时。
新建TimeView类,并扩展自LinearLayout,然后布局文件和上一篇中那么写就好了。
// 实例化TextView控件
private TextView tvTime;
public TimeView(Context context) {
super(context);
}
public TimeView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TimeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate(){
super.onFinishInflate();
tvTime = (TextView)findViewById(R.id.tvTime);
tvTime.setText(H);
timeHandler.sendEmptyMessage(0);
}
private Handler timeHandler = new Handler() {
public void handleMessage(Message msg){
// 刷新时间
refreshTime();
if(getVisibility() == View.VISIBLE){
timeHandler.sendEmptyMessageDelayed(0, 1000);
}
}
};
// 刷新时间
private void refreshTime(){
Calendar c = Calendar.getInstance();
// 试着显示的时间格式
tvTime.setText(String.format(%d:%d:%d,
c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
c.get(Calendar.SECOND)));
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if(visibility == View.VISIBLE){
timeHandler.sendEmptyMessage(0);
}else{
timeHandler.removeMessages(0);
}
}
就这些了,下面正式开始计时器的部分。
TimerView
TimerView类同样是扩展自LinearLayout,并且布局的写法也是如出一辙:
android:singleLine = true / >
< / LinearLayout>
< / LinearLayout>
先把该定义的都定义好了:
private Button btnStart, btnPause, btnResume, btnReset;
private EditText etHour, etMinute, etSecond;
private static final int MSG_WHAT_TIME_IS_UP = 1;
private static final int MSG_WHAT_TIME_TICK = 2;
// 所有时间计数
private int allTimerCount = 0;
private Timer timer = new Timer();
private TimerTask timerTask = null;
public TimerView(Context context) {
super(context);
}
public TimerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TimerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
核心部分,首先给各个按钮设置监听,其中的点击分别实现相应的功能,并且设置相应的可见度,还要为每个EditText设置一个动态的判断,使其值不大于59也不小于0。
@Override
protected void onFinishInflate(){
super.onFinishInflate();
// 暂停
btnPause = (Button)findViewById(R.id.btnPause);
btnPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopTimer();
btnPause.setVisibility(View.GONE);
btnResume.setVisibility(View.VISIBLE);
}
});
// 重置
btnReset = (Button)findViewById(R.id.btnReset);
btnReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopTimer();
etHour.setText(0);
etMinute.setText(0);
etSecond.setText(0);
btnReset.setVisibility(View.GONE);
btnResume.setVisibility(View.GONE);
btnPause.setVisibility(View.GONE);
btnStart.setVisibility(View.VISIBLE);
}
});
// 恢复
btnResume = (Button)findViewById(R.id.btnResume);
btnResume.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startTime();
btnResume.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
});
// 开始
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startTime();
btnStart.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
btnReset.setVisibility(View.VISIBLE);
}
});
etHour = (EditText)findViewById(R.id.etHour);
etMinute = (EditText)findViewById(R.id.etMinute);
etSecond = (EditText)findViewById(R.id.etSecond);
// 对每一个EditText实例都作判断,值不可以大于59或小于0
etHour.setText(00);
etHour.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!TextUtils.isEmpty(s)) {
int value = Integer.parseInt(s.toString());
if (value > 59) {
etHour.setText(59);
} else if (value < 0) {
etHour.setText(0);
}
}
checkToEnableBtnStart();
}
@Override
public void afterTextChanged(Editable s) {
}
});
etMinute.setText(00);
etMinute.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!TextUtils.isEmpty(s)) {
int value = Integer.parseInt(s.toString());
if (value > 59) {
etMinute.setText(59);
} else if (value < 0) {
etMinute.setText(0);
}
}
checkToEnableBtnStart();
}
@Override
public void afterTextChanged(Editable s) {
}
});
etSecond.setText(00);
etSecond.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!TextUtils.isEmpty(s)){
int value = Integer.parseInt(s.toString());
if(value > 59){
etSecond.setText(59);
}else if(value < 0){
etSecond.setText(0);
}
}
checkToEnableBtnStart();
}
@Override
public void afterTextChanged(Editable s) {
}
});
btnStart.setVisibility(View.VISIBLE);
btnPause.setVisibility(View.GONE);
btnResume.setVisibility(View.GONE);
btnReset.setVisibility(View.GONE);
}
相关报道:
一旦你决定开始一个博客,你需要为博客确定你的目标是什么。如果你想发展你的博客,吸引新读者(并保持他们一直访问),你就需要花一些心思到您的博客,采用考虑一下什么样的发帖频率。 博客内容是关键 在博客世界里,常用的一句话是内容为王。这就意味着你的 更多
OpenCV 学习(像素操作 Manipuating the Pixels)OpenCV 虽然提供了许多类型的图像处理函数,可以对图像进行各种常见的处理,但是总会有些操作时没有的,这时我 更多