Android uses timer to write countdown program
Last Update:2015-08-28
Source: Internet
Author: User
<span id="Label3"></p><p><p>Let's start by looking at the basic usage of the timer and the simple Principle. http://my.oschina.net/zhengweishan/blog/493891 Java Timer Used. What I'm saying here is that Android uses a timer to write a countdown Program.</p></p><p><p>Requirements: implement a simple countdown Program. Requirements can be based on the User's input to achieve the countdown, time to the time there are friendly tips.</p></p><p><p>Analysis: <span style="line-height: 22.5px;">First</span> to achieve this function, I think of the first method is to use the timer class. Then it is the analysis of the user interface design, because this is a simple program, so the use of some Android native UI components button TextView EditText to design the basic Page. Friendly hints I intend to use this mechanism with Toasts. This is intended to allow users to enter SECONDS.</p></p><p><p>Source: Https://git.oschina.net/zhengweishan/AndroidTimer</p></p><p><p>Activity code:</p></p><p><p>Import java.util.Timer;</p></p><p><p>Import java.util.TimerTask;</p></p><p><p><br></p></p><p><p>Import android.app.Activity;</p></p><p><p>Import android.os.Bundle;</p></p><p><p>Import android.os.Handler;</p></p><p><p>Import android.os.Message;</p></p><p><p>Import android.view.View;</p></p><p><p>Import android.view.View.OnClickListener;</p></p><p><p>Import android.widget.Button;</p></p><p><p>Import android.widget.EditText;</p></p><p><p>Import android.widget.TextView;</p></p><p><p>Import android.widget.Toast;</p></p><p><p><br></p></p><p><p>public class Mainactivity extends Activity implements Onclicklistener {</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Private Button startbutton;</p></p><p><p><span style="white-space:pre"></span>Private Button stopbutton;</p></p><p><p><span style="white-space:pre"></span>Private EditText minutetext;</p></p><p><p><span style="white-space:pre"></span>Private EditText secondtext;</p></p><p><p><span style="white-space:pre"></span>Private TextView myTime;</p></p><p><p><span style="white-space:pre"></span>private int minute = 0;</p></p><p><p><span style="white-space:pre"></span>private int second = 0;</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Private Timer timer = null;</p></p><p><p><span style="white-space:pre"></span>Private TimerTask task = null;</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>protected void OnCreate (Bundle Savedinstancestate) {</p></p><p><p><span style="white-space:pre"></span>Super.oncreate (savedinstancestate);</p></p><p><p><span style="white-space:pre"></span>Setcontentview (r.layout.activity_main);</p></p><p><p><span style="white-space:pre"></span>Initview ();</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>private void Initview () {</p></p><p><p><span style="white-space:pre"></span>Startbutton = (Button) Findviewbyid (r.id.startbtn);</p></p><p><p><span style="white-space:pre"></span>Stopbutton = (Button) Findviewbyid (r.id.stopbtn);</p></p><p><p><span style="white-space:pre"></span>Minutetext = (EditText) Findviewbyid (r.id.minute);</p></p><p><p><span style="white-space:pre"></span>Secondtext = (EditText) Findviewbyid (r.id.second);</p></p><p><p><span style="white-space:pre"></span>MyTime = (TextView) Findviewbyid (r.id.mytime);</p></p><p><p><span style="white-space:pre"></span>Startbutton.setonclicklistener (this);</p></p><p><p><span style="white-space:pre"></span>Stopbutton.setonclicklistener (this);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>public void OnClick (View V) {</p></p><p><p><span style="white-space:pre"></span>TODO auto-generated Method Stub</p></p><p><p><span style="white-space:pre"></span>Switch (v.getid ()) {</p></p><p><p><span style="white-space:pre"></span>Case R.id.startbtn:</p></p><p><p><span style="white-space:pre"></span>Start ();</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Case R.id.stopbtn:</p></p><p><p><span style="white-space:pre"></span>Stop ();</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>public void start () {</p></p><p><p><span style="white-space:pre"></span>If (!minutetext.gettext (). toString (). equals ("")) {</p></p><p><p><span style="white-space:pre"></span>minute = Integer.parseint (minutetext.gettext (). toString ());</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>If (!secondtext.gettext (). toString (). equals ("")) {</p></p><p><p><span style="white-space:pre"></span>Second = Integer.parseint (secondtext.gettext (). toString ());</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Mytime.settext (minute + ":" + second);</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>task = new TimerTask () {</p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>public void Run () {</p></p><p><p><span style="white-space:pre"></span>Message msg = new Message ();</p></p><p><p><span style="white-space:pre"></span>Msg.what = 0;</p></p><p><p><span style="white-space:pre"></span>Mhandler.sendmessage (msg);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>};</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Timer = new Timer ();</p></p><p><p><span style="white-space:pre"></span>Timer.schedule (task, 0, 1000);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>public void Stop () {</p></p><p><p><span style="white-space:pre"></span>Timer.cancel ();</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>public void timeprocess () {</p></p><p><p><span style="white-space:pre"></span>if (minute = = 0) {</p></p><p><p><span style="white-space:pre"></span>if (second = = 0) {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("time out!");</p></p><p><p><span style="white-space:pre"></span>Toast.maketext (getapplicationcontext (), "time out!", 5);</p></p><p><p><span style="white-space:pre"></span>If (timer! = Null) {</p></p><p><p><span style="white-space:pre"></span>Timer.cancel ();</p></p><p><p><span style="white-space:pre"></span>Timer = null;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>If (task! = Null) {</p></p><p><p><span style="white-space:pre"></span>Task = null;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>second--;</p></p><p><p><span style="white-space:pre"></span>If (second >= 10) {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("0" + minute + ":" + second);</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("0" + minute + ": 0" + second);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>if (second = = 0) {</p></p><p><p><span style="white-space:pre"></span>Second = 59;</p></p><p><p><span style="white-space:pre"></span>minute--;</p></p><p><p><span style="white-space:pre"></span>If (minute >= 10) {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext (minute + ":" + second);</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("0" + minute + ":" + second);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>second--;</p></p><p><p><span style="white-space:pre"></span>If (second >= 10) {</p></p><p><p><span style="white-space:pre"></span>If (minute >= 10) {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext (minute + ":" + second);</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("0" + minute + ":" + second);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>If (minute >= 10) {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext (minute + ": 0" + second);</p></p><p><p><span style="white-space:pre"></span>} else {</p></p><p><p><span style="white-space:pre"></span>Mytime.settext ("0" + minute + ": 0" + second);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Private Handler Mhandler = new Handler () {</p></p><p><p><span style="white-space:pre"></span>public void Handlemessage (Message Msg) {</p></p><p><p><span style="white-space:pre"></span>Timeprocess ();</p></p><p><p><span style="white-space:pre"></span>};</p></p><p><p><span style="white-space:pre"></span>};</p></p><p><p>}</p></p><p><p>XML Layout File:</p></p><p><p><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"</p></p><p><p>xmlns:tools= "http://schemas.android.com/tools"</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>Android:paddingbottom= "@dimen/activity_vertical_margin"</p></p><p><p>android:paddingleft= "@dimen/activity_horizontal_margin"</p></p><p><p>android:paddingright= "@dimen/activity_horizontal_margin"</p></p><p><p>android:paddingtop= "@dimen/activity_vertical_margin"</p></p><p><p>tools:context= "com.example.androidtimer.MainActivity" ></p></p><p><p><br></p></p><p><p><textview</p></p><p><p>Android:id= "@+id/lable"</p></p><p><p>Android:layout_width= "wrap_content"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>android:layout_alignparentleft= "true"</p></p><p><p>android:layout_alignparenttop= "true"</p></p><p><p>android:text= "@string/lable"/></p></p><p><p><br></p></p><p><p><edittext</p></p><p><p>Android:id= "@+id/minute"</p></p><p><p>Android:layout_width= "100dp"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>android:layout_alignleft= "@+id/lable"</p></p><p><p>android:layout_below= "@+id/lable"</p></p><p><p>android:layout_margintop= "40dp"</p></p><p><p>android:ems= "ten"/></p></p><p><p><br></p></p><p><p><textview</p></p><p><p>Android:id= "@+id/textview2"</p></p><p><p>Android:layout_width= "wrap_content"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>Android:layout_alignbottom= "@+id/minute"</p></p><p><p>android:layout_torightof= "@+id/minute"</p></p><p><p>android:text= "@string/minute"/></p></p><p><p><br></p></p><p><p><edittext</p></p><p><p>Android:id= "@+id/second"</p></p><p><p>Android:layout_width= "100dp"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>android:layout_torightof= "@+id/textview2"</p></p><p><p>android:layout_below= "@+id/lable"</p></p><p><p>android:layout_margintop= "40dp"</p></p><p><p>android:ems= "ten"/></p></p><p><p><br></p></p><p><p><textview</p></p><p><p>Android:id= "@+id/textview3"</p></p><p><p>Android:layout_width= "wrap_content"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>android:layout_torightof= "@+id/second"</p></p><p><p>Android:layout_alignbottom= "@+id/second"</p></p><p><p>android:text= "@string/second"/></p></p><p><p></p></p><p><p><textview</p></p><p><p>Android:id= "@+id/mytime"</p></p><p><p>Android:layout_width= "fill_parent"</p></p><p><p>android:layout_height= "fill_parent"</p></p><p><p>Android:layout_above= "@+id/startbtn"</p></p><p><p>android:layout_alignright= "@+id/textview3"</p></p><p><p>android:layout_margin= "30dp"</p></p><p><p>android:gravity= "center"</p></p><p><p>Android:textcolor= "#FF0000"</p></p><p><p>Android:textsize= "60sp"</p></p><p><p>Android:textstyle= "bold"/></p></p><p><p></p></p><p><p><button</p></p><p><p>Android:id= "@+id/startbtn"</p></p><p><p>Android:layout_width= "wrap_content"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>android:layout_alignleft= "@+id/minute"</p></p><p><p>Android:layout_alignparentbottom= "true"</p></p><p><p>android:text= "@string/startbtn"/></p></p><p><p><br></p></p><p><p><button</p></p><p><p>Android:id= "@+id/stopbtn"</p></p><p><p>Android:layout_width= "wrap_content"</p></p><p><p>android:layout_height= "wrap_content"</p></p><p><p>Android:layout_alignbottom= "@+id/startbtn"</p></p><p><p>android:layout_torightof= "@+id/startbtn"</p></p><p><p>android:layout_alignright= "@+id/textview3"</p></p><p><p>android:layout_marginright= "40dp"</p></p><p><p>android:text= "@string/stopbtn"/></p></p><p><p></RelativeLayout></p></p><p><p>String.xml file:<br></p></p><p><p><?xml version= "1.0" encoding= "utf-8"?></p></p><p><p><resources></p></p><p><p><br></p></p><p><p><string name= "app_name" >AndroidTimer</string></p></p><p><p><string name= "hello_world" >hello world!</string></p></p><p><p><string name= "action_settings" >Settings</string></p></p><p><p><string name= "lable" > Please set the time:</string></p></p><p><p><string name= "minute" > min:</string></p></p><p><p><string Name= "second" > Sec </string></p></p><p><p><string name= "startbtn" > Start Timing </string></p></p><p><p><string name= "stopbtn" > Stop Timing </string></p></p><p><p><br></p></p><p><p></resources></p></p><p><p><br></p></p><p><p>Android uses timer to write countdown program</p></p></span>