Multi-threaded review of Android Learning-Countdown

Source: Internet
Author: User

Today Saturday, leave home, review a bit of multithreading knowledge

Then wrote a simple countdown program:

Here is the layout file for my activity_main:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent" 5 android:orientation= "vertical">6 7     <EditText8         Android:layout_width= "Fill_parent"9 Android:layout_height= "Wrap_content"Ten Android:id= "@+id/ettime"/> One      A     <Button -         Android:id= "@+id/btngettime" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" - Android:text= "@string/counttime" /> -  -     <TextView +         Android:id= "@+id/tvshowtime" - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" A Android:text="" /> at  -     <Button -         Android:id= "@+id/btnstart" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "@string/start" /> in  -     <Button to         Android:id= "@+id/btnstop" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/stop" /> *  $ </LinearLayout>
View Code

It's a edittext, a TextView, three buttons, No.
And then the code for my mainactivity:

1 Package Com.oysd.counttime;2 3 import Java.util.Timer;4 import Java.util.TimerTask;5 6 import android.app.Activity;7 import Android.os.Bundle;8 import Android.os.Handler;9 import android.os.Message;Ten import Android.view.Menu; One import Android.view.MenuItem; A import Android.view.View; - import Android.view.View.OnClickListener; - import Android.widget.Button; the import Android.widget.EditText; - import Android.widget.TextView; -  - Public class Mainactivity extends Activity implements Onclicklistener { +      - //Declare the controls you want to use + private EditText ettime; A private Button btngettime,btnstart,btnstop; at private TextView tvshowtime; - private int i = 0; - private timer timer; - private TimerTask task; -  - private Handler Mhandler = new Handler () { in Public void Handlemessage (Android.os.Message msg) { - Tvshowtime.settext (Msg.arg1 + ""); to startTime (); +         }; -     }; the      * @Override $ protected void OnCreate (Bundle savedinstancestate) {Panax Notoginseng super.oncreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the Initview (); +     } A      the private void Initview () { + ettime = (EditText) Findviewbyid (r.id.ettime); - tvshowtime = (TextView) Findviewbyid (r.id.tvshowtime); $ btngettime = (Button) Findviewbyid (r.id.btngettime); $ btnstart = (Button) Findviewbyid (R.id.btnstart); - btnstop = (Button) Findviewbyid (r.id.btnstop); -          the Btngettime.setonclicklistener (this); - Btnstart.setonclicklistener (this);Wuyi Btnstop.setonclicklistener (this); the     } -  Wu @Override - Public void OnClick (View v) { About switch (V.getid ()) { $ Case R.id.btngettime: - if (Ettime.gettext (). toString (). Equals ("")) { - Tvshowtime.settext (0+ ""); - i = 0; A }else{ + Tvshowtime.settext (Ettime.gettext (). toString ()); the i = Integer.parseint (Tvshowtime.gettext (). toString ()); -             } $ Break ; the Case R.id.btnstart: the startTime (); the Break ; the Case R.id.btnstop: - stoptime (); in Break ; the         } the     } About      the private void StartTime () { the timer = new timer (); the task = new TimerTask () { +              - @Override the Public void Run () {Bayi i--; the Message message = new Message (); the message.arg1 = i; - mhandler.sendmessage (message); -             } the         }; the Timer.schedule (task, +); the     } the      - private void StopTime () { the Timer.cancel (); the     } the}
View Code

In the child thread, do not attempt to modify the main thread UI thread, otherwise it will cause the interface to die, need to use Handlemessage to manipulate the UI thread

In addition, using a timer, you must also use timertask, to achieve the countdown time

Multi-threaded review of Android Learning-Countdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.