Android_handler simple to use, test

Source: Internet
Author: User

package yjm.com.testcontrol;import android.app.activity;import android.os.bundle;import  android.os.handler;import android.os.message;import android.view.view;import  Android.widget.button;import android.widget.textview;public class testactivity extends  Activity implements View.OnClickListener{    TextView tv1;     textview tv2;    button btn1;    button  btn2;    //Create  ui thread update data, primarily ui  control   does not support asynchronous thread updates, all values must be in the UI thread value to be modified       handler myhandler = new handler () {           @Override          public void  Handlemessage (message msg)  {              switch  (msg.what) {&NBSP;&NBSP;&NBSP;&Nbsp;             case r.id.tv1:                       tv1.settext ("+msg.arg1+" ");                      break;                  case R.id.tv2:                      tv2.settext ("" +msg.arg2+ "");             }              super.handlemessage (msg);          }     };     class  theadtest extends thread{         handler myhandler;          message msg;         public  theadtest (Handler handler) {               this.myHandler=handler;         }          @Override         public void  Run () {            for  (int i =  0; i <100 ; i++)  {                 try {                     sleep (;     )            } catch  (interruptedexception e)  {                      E.printstacktrace ();                 }                // This message  object must be new. If you send the same one repeatedly, you will get stuck with an error.                 msg= new  message ();                 msg.what=R.id.tv1;                 msg.arg1=i;                 myhandler.sendmessage (msg);             }         }    }     @Override     protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (r.layout.main_test);/**/        tv1= ( TextView) Findviewbyid (R.ID.TV1);         tv2= (TextView) Findviewbyid ( R.ID.TV2)         btn1= (Button) Findviewbyid (R.ID.BTN1);         btn2= (Button) Findviewbyid (R.ID.BTN2);         btn1.setonclicklistener (This);         Btn2.setonclicklistener (This);    }     @Override      public void onclick (View vieW)  {        if (View.getid () ==r.id.btn1) {             tv1.settext ("Btn1_test. Start. ");             new theadtest (MyHandler). Start ();         }        // UI data volume update in UI thread test   button 2 test         if (View.getid () ==r.id.btn2) {             for  (int i = 0;  i <10000 ; i++)  {                 tv2.settext ("Btn2_test ...:" +i);             }        }    }}


TV1 in the main thread 1-99 display

TV1 asynchronously updates values, displaying values by handler

Android_handler simple to use, test

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.