博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Handler详细说明系列(六)——View的post()详解
阅读量:7026 次
发布时间:2019-06-28

本文共 2206 字,大约阅读时间需要 7 分钟。

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例如以下:

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

你可能感兴趣的文章
ubuntu 步步为营之uclinux编译和移植(完整版)
查看>>
Lintcode: Partition Array
查看>>
sudo 之后 unable to resolve host的问题解决办法
查看>>
那些PHP中没有全称的简写
查看>>
【elasticsearch】python下的使用
查看>>
python字符串和编码
查看>>
JS实现表单多文件上传样式美化支持选中文件后删除相关项
查看>>
高可用高并发常用到的9种技术
查看>>
数字签名
查看>>
SQL Server数据库中批量替换数据的方法
查看>>
QTP 浏览器最大化、最小化,适用于IE6\7\8
查看>>
Androidの遇到的问题集合之MaginPadding
查看>>
hdu 1856 并差集求最大秩
查看>>
线段树
查看>>
框架技术--Spring自动加载配置
查看>>
画之国 Le tableau (2011)
查看>>
jquery淡入淡出无延迟代码
查看>>
js 规范
查看>>
OpenCV入门学习(三)HistogramEquivalent
查看>>
Intellij IDEA 10.5 语言设置
查看>>