zoukankan      html  css  js  c++  java
  • Handler详细说明系列(六)——View的post()详解

    MainActivity例如下列:
    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描写叙述:
     * 在子线程中更改UI的方式二
     * 
     * 在子线程中採用View的post()方法.
     * 根据源代码可知它在终于还是调用了UI线程的handler的post()方法.
     * 所以在post方法中勿做耗时操作
     * 
     * 看一下该方法的源代码:
     *
     * 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
     *         looper 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;
     * }
     * 
     * 
     * 该方法的英文凝视:
     * 将Runnable放入消息队列,并在UI线程中运行.
     * 
     * 參考资料:
     * 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;
    		@Override
    		protected 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线程ID="+Thread.currentThread().getId());
    	    }
    		
    	private class OnClickListenerImpl implements OnClickListener {
    		@Override
    		public void onClick(View v) {
    			mTipTextView.post(new Runnable() {
    				@Override
    				public void run() {
    					mTextView.setText("My number is 9527");
    					System.out.println("view.post(new Runnable())里的run()方法 线程ID="+Thread.currentThread().getId());
    				}
    			});
    		}
    	}
    }
    

    main.xml例如以下:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
    
         <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dip"
            />
         
         <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="120dip"
            />
        
        <TextView
            android:id="@+id/tipTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="測试在子线程中更新UI" 
            android:layout_centerInParent="true"
            />
    
    </RelativeLayout>


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    自我介绍
    币值转换
    打印沙漏
    对我影响最大的三位老师

    pta
    pta-3
    学习计划
    对我有影响的三个老师
    介绍自己
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4746153.html
Copyright © 2011-2022 走看看