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: