功能
- 输入会及时显示并且检查类型错误
- 二次输入会自动销毁已经开始的计时任务
- 暂停后点击开始能重新开始未完成的计时任务
- 当计时结束后,不能重复相同的计时任务,须重新输入
实现
- 逻辑代码(若理解有误,欢迎指正)
package com.damnever.counttime;
/*
* @Time: 14-11-22 16:41 Fri
* @author: Damnever
* @Error: Only the original thread that created a view hierarchy can touch its views.
* @Reason: 只有UI线程(主线程,是线程不安全的)能更新组件,子线程(计时)不能直接更新UI组件,*冲突*
* @Solution: A *Handler* allows you to send and process Message and Runnable objects associated with a thread's
* MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When
* you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from
* that point on, it will deliver messages and runnables to that message queue and execute them as they come out
* of the message queue.
* Handler可以将子线程的消息关联到主线程的消息队列,这样主线程就能处理来自子线程的消息
*/
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import androi