Handler详解系列(六)——View的post()方法详解

简介: MainActivity如下:package cc.testui2;import android.os.Bundle;import android.
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>


相关文章
|
2月前
|
消息中间件 调度 Android开发
Android经典面试题之View的post方法和Handler的post方法有什么区别?
本文对比了Android开发中`View.post`与`Handler.post`的使用。`View.post`将任务加入视图关联的消息队列,在视图布局后执行,适合视图操作。`Handler.post`更通用,可调度至特定Handler的线程,不仅限于视图任务。选择方法取决于具体需求和上下文。
40 0
|
4月前
|
前端开发 搜索推荐 JavaScript
Spartacus Cart item 点击了 remove 之后 HTTP Delete 请求的触发逻辑 - Adapter
Spartacus Cart item 点击了 remove 之后 HTTP Delete 请求的触发逻辑 - Adapter
|
4月前
通过 Filter 与包装 HttpServletRequest 添加自定义 header
通过 Filter 与包装 HttpServletRequest 添加自定义 header
255 0
|
XML 网络安全 Android开发
Volley学习笔记 | 关于源码中Request、Response、Listener泛型的理解(附XMLRequest自定义代码)
Volley学习笔记 | 关于源码中Request、Response、Listener泛型的理解(附XMLRequest自定义代码)
|
Kotlin
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
301 0
|
消息中间件 Android开发
【Android 异步操作】手写 Handler ( Handler 发送与处理消息 | Handler 初始化 | 完整 Handler 代码 )
【Android 异步操作】手写 Handler ( Handler 发送与处理消息 | Handler 初始化 | 完整 Handler 代码 )
144 0
|
Web App开发 数据安全/隐私保护 缓存
|
消息中间件 Android开发 调度