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>

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

Handler详细说明系列(六)——View的post()详解的更多相关文章

  1. Solr系列六:solr搜索详解优化查询结果(分面搜索、搜索结果高亮、查询建议、折叠展开结果、结果分组、其他搜索特性介绍)

    一.分面搜索 1. 什么是分面搜索? 分面搜索:在搜索结果的基础上进行按指定维度的统计,以展示搜索结果的另一面信息.类似于SQL语句的group by 分面搜索的示例: http://localhos ...

  2. Lucene系列六:Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)

    一.搜索流程详解 1. 先看一下Lucene的架构图 由图可知搜索的过程如下: 用户输入搜索的关键字.对关键字进行分词.根据分词结果去索引库里面找到对应的文章id.根据文章id找到对应的文章 2. L ...

  3. SpringBoot系列(六)集成thymeleaf详解版

    SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介  1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎.  2. Thymeleaf ...

  4. 第十六章 IIC协议详解+UART串口读写EEPROM

    十六.IIC协议详解+Uart串口读写EEPROM 本文由杭电网友曾凯峰根据小梅哥FPGA IIC协议基本概念公开课内容整理并最终编写Verilog代码实现使用串口读写EEPROM的功能. 以下为原文 ...

  5. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  6. [转]PostgreSQL教程(十六):系统视图详解

    这篇文章主要介绍了PostgreSQL教程(十六):系统视图详解,本文讲解了pg_tables.pg_indexes.pg_views.pg_user.pg_roles.pg_rules.pg_set ...

  7. [转帖]Linux系列之SAR命令使用详解

    Linux系列之SAR命令使用详解 sar是System Activity Reporter(系统活动情况报告)的缩写.这个工具所需要的负载很小,也是目前linux中最为全面的性能分析工具之一.此款工 ...

  8. 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)> 介绍了properties与environments, ...

  9. Android Studio系列教程五--Gradle命令详解与导入第三方包

    Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...

随机推荐

  1. 同一个PC只能运行一个应用实例(考虑多个用户会话情况)

    原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) class Program { private static Mutex m; [STAThread] static void Main( ...

  2. 在用TabbarController中出现navigationController 嵌套报错

    假设出现: nested push animation can result in corrupted navigation bar Finishing up a navigation transit ...

  3. 【 D3.js 进阶系列 — 1.1 】 其它表格文件的读取

    CSV 表格文件是以逗号作为单元分隔符的,其他还有以制表符 Tab 作为单元分隔符的 TSV 文件,还有人为定义的其他分隔符的表格文件.本文将说明在 D3 中怎样读取它们. 1. TSV 表格文件是什 ...

  4. Andrew Stankevich&#39;s Contest (1)

    Andrew Stankevich's Contest (1) 打一半出门了,回来才补完了...各种大数又不能上java..也是蛋疼无比 A:依据置换循环节非常easy得出要gcd(x, n) = 1 ...

  5. VS2015在对GIT的支持

    VS2015在对GIT的支持 相比VS2013,VS2015在对GIT的支持上有了更强大的支持.本篇仅作抛砖引玉,不做过多介绍: 1. 打开VS 2015起始页 2. 打开团队资源管理器 打开[本地G ...

  6. 安装numpy、nltk问题汇总

    系统环境是win7(64bit)+python3.4(64bit)+numpy1.82+vs2012 1.假设用sourceforge上编译好的32bit的exe安装,会提示'python versi ...

  7. FZU1669 Right-angled Triangle【毕达哥拉斯三元组】

    主题链接: pid=1669">http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a.b为直角边,c为斜边,而且满足a + b ...

  8. 使用shell/python获取hostname/fqdn释疑(转)

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  9. RH253读书笔记(2)-Lab 2 System Resource Access Controls

    Lab 2 System Resource Access Controls Goal: To become familiar with system resource access controls. ...

  10. ACdreamoj1110(多重背包)

    意甲冠军:多个裸露的双肩背包.水的问题. 解决方法:然背包一样,仅仅只是加一个数组,记录着每一个物品用过的次数,多于存储量时就pass不更新. 另一种方法是将每一个物品用二进制压缩处理.第一个代码比較 ...