The sixth bullet in Android development: simple clock (timer)
Next to the fifth bullet in Android development: simple clock (alarm clock), this time is a clock application, and it is still the main function, still very frustrated. Of course, there are some core functions ......
Clock
First, let's list the simple clock. It's all very simple here. Even if we want to use all the cities in the world, we just add or subtract a few hours.
Create a new TimeView class and extend it from LinearLayout. Then, layout the file and write it in the previous article.
// Instantiate the TextView control 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) ;}}; // refresh time private void refreshTime () {Calendar c = Calendar. getInstance (); // try to display the time format tvTime. setText (String. format (% 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 );}}
Now, the timer section is officially started below.
TimerView
The TimerView class is also extended from LinearLayout, and the layout is also written in the same way:
android:singleLine = true / >
< / LinearLayout>
< / LinearLayout>
Define all the definitions first:
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; // all time count 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 );}
The core part is to set the listener for each button. Click to implement the corresponding functions, set the corresponding visibility, and set a dynamic judgment for each EditText, make the value not greater than 59 or less than 0.
@ Override protected void onFinishInflate () {super. onFinishInflate (); // pause 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) ;}}); // reset btnReset = (Button) findViewById (R. id. btnReset); btnReset. setOnClickListener (new OnClick Listener () {@ 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) ;}}); // restore btnResume = (Button) findViewById (R. id. btnResume); btnResume. setOnClickListener (new OnClickListener () {@ Override publ Ic void onClick (View v) {startTime (); btnResume. setVisibility (View. GONE); btnPause. setVisibility (View. VISIBLE) ;}}); // start 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) ;}}); etHou R = (EditText) findViewById (R. id. etHour); etMinute = (EditText) findViewById (R. id. etMinute); etSecond = (EditText) findViewById (R. id. etSecond); // judge every EditText instance. The value cannot be greater than 59 or less than 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 af Ter) {}@ 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 );}
Determines whether the count can be started. Each input box cannot be smaller than or equal to 0.
// Determine whether private void checkToEnableBtnStart () {btnStart. setEnabled ((! TextUtils. isEmpty (etHour. getText () & Integer. parseInt (etHour. getText (). toString ()> 0) | (! TextUtils. isEmpty (etMinute. getText () & Integer. parseInt (etMinute. getText (). toString ()> 0) | (! TextUtils. isEmpty (etSecond. getText () & Integer. parseInt (etSecond. getText (). toString ()> 0 ));}
Next we can start timing.
// Start private void startTime () {if (timerTask = null) {// obtain the total number of seconds to be counted from the three input boxes allTimerCount = Integer. parseInt (etHour. getText (). toString () * 60*60 + Integer. parseInt (etMinute. getText (). toString () * 60 + Integer. parseInt (etSecond. getText (). toString (); timerTask = new TimerTask () {@ Override // execution count, allTimerCount auto minus public void run () {allTimerCount --; handler. sendEmptyMessage (MSG_WHAT_TIME_TICK); // if all the remaining counts are smaller than 0, the handler is notified to stop if (allTimerCount <= 0) {handler. sendEmptyMessage (MSG_WHAT_TIME_IS_UP); stopTimer () ;}}; // sets scedule, start time, and interval. The interval is 1 second timer. schedule (timerTask );}}
Of course, in addition to starting timing, you also need to be able to stop timing.
// Stop timing private void stopTimer () {if (timerTask! = Null) {timerTask. cancel (); timerTask = null ;}}
The next step is Handler, which is not difficult. Just write it several more times.
Private Handler handler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case MSG_WHAT_TIME_TICK: // get time int hour = allTimerCount/60/60; int min = (allTimerCount/60) % 60; int sec = allTimerCount % 60; // write the time To etHour on the corresponding EditText. setText (hour +); etMinute. setText (min +); etSecond. setText (sec +); break; case MSG_WHAT_TIME_IS_UP: // a prompt is displayed in the pop-up dialog box, including the title, message, and return button new AlertDialog. builder (getContext ()). setTitle (Time is up ). setMessage (Message: Time is up ). setNegativeButton (Cancel, null ). show (); // set whether to be visible or not. btnReset. setVisibility (View. GONE); btnResume. setVisibility (View. GONE); btnPause. setVisibility (View. GONE); btnStart. setVisibility (View. VISIBLE); break; default: break ;}}};
End
This is the second article. There is also a short article. The seventh bullet in Android development: simple clock (stopwatch )......
If you need code, leave a comment to your mailbox. I will not upload the code to CSDN resources. The code will be updated, and the comments will be updated ......