Handler series (6)-View post () method

Source: Internet
Author: User

Handler series (6)-View post () method
MainActivity is as follows:

Package cc. testui2; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. app. activity;/*** Demo Description: * method for changing the UI in a child thread ** the post () method of the View is used in the child thread. * Based on the source code, we can see whether it finally calls the post () method of the handler of the UI thread. * do not perform time-consuming operations in the post method ** check the source code of this method: ** Causes the Runnable to be added to the message queue. * The runnable Will be run on the user interface thread. ** @ param action The Runnable that will be executed. ** @ return Returns true if the Runnable was successfully placed in to the * message queue. returns false on failure, usually because the * logoff processing the message queue is exiting. ** public boolean post (Runnable action) {* final AttachInfo attachInfo = mAttachInfo; * if (attachInfo! = Null) {* return attachInfo. mHandler. post (action); *} * // Assume that post will succeed later * ViewRootImpl. getRunQueue (). post (action); * return true; *} *** English comment of this method: * put Runnable into the message queue and execute it in the UI thread. ** reference: * http://blog.csdn.net/guolin_blog/article/details/9991569 * Thank you very much */public class MainActivity extends Activity {private TextView mTextView; private TextView mTipTextView; private Button mButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mTextView = (TextView) findViewById (R. id. textView); mTipTextView = (TextView) findViewById (R. id. tipTextView); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new OnClickListenerImpl (); System. out. println ("UI Thread ID =" + Thread. currentThread (). getId ();} private class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {mTipTextView. post (new Runnable () {@ Overridepublic void run () {mTextView. setText ("My number is 9527"); System. out. println ("view. post (new Runnable () in the run () method Thread ID = "+ Thread. currentThread (). getId ());}});}}}

Main. xml is as follows:
      
            
          
  
 


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.