MainActivity例如以下:

package cc.testui3;
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的方式三
*
* 调用Activity.runOnUiThread(Runnable runnable)方法.
*
*该方法的源代码例如以下:
*
* Runs the specified action on the UI thread. If the current thread is the UI
* thread, then the action is executed immediately. If the current thread is
* not the UI thread, the action is posted to the event queue of the UI thread.
*
* @param action the action to run on the UI thread
* public final void runOnUiThread(Runnable action) {
* if (Thread.currentThread() != mUiThread) {
* mHandler.post(action);
* } else {
* action.run();
* }
* }
*
*
* 源代码中的这段凝视太具体和贴心了,赞一个!
* 在UI线程中运行该Runnable.
* 假设当前线程是UI线程,那么该Runnable会马上运行.
* 假设当前的线程不是UI线程则调用UI线程handler的post()方法将其放入UI线程的消息队列中.
* 注意:勿在runOnUiThread(Runnable runnable)中做耗时操作
*
* 參考资料:
* http://blog.csdn.net/guolin_blog/article/details/9991569
* Thank you very much
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Activity mActivity;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mActivity=this;
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
} private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
new Thread(){
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("My name is zxc , number is 007");
}
});
};
}.start();
}
} }

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具体解释系列(七)——Activity.runOnUiThread()方法具体解释的更多相关文章

  1. android Activity runOnUiThread() 方法使用

    在android 中我们一般用 Handler 做主线程 和 子线程 之间的通信 . 现在有了一种更为简洁的写法,就是 Activity 里面的 runOnUiThread( Runnable )方法 ...

  2. android Activity runOnUiThread() 方法的使用

    利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThr ...

  3. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  4. activity.runOnUiThread()内的run()方法没有被执行

    activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, toast, Toast.LEN ...

  5. Android Studio 单刷《第一行代码》系列 04 —— Activity 相关

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  6. 理解Activity.runOnUiThread()

    这是一篇译文(中英对照),原文链接:Understanding Activity.runOnUiThread() When developing Android applications we alw ...

  7. 【转】Android总结篇系列:Activity生命周期

    [转]Android总结篇系列:Activity生命周期 Android官方文档和其他不少资料都对Activity生命周期进行了详细介绍,在结合资料和项目开发过程中遇到的问题,本文将对Activity ...

  8. iOS流布局UICollectionView系列七——三维中的球型布局

      摘要: 类似标签云的球状布局,也类似与魔方的3D布局 iOS流布局UICollectionView系列七——三维中的球型布局 一.引言 通过6篇的博客,从平面上最简单的规则摆放的布局,到不规则的瀑 ...

  9. 理解 Activity.runOnUiThread

    在开发 Android 应用的时候我们总是要记住应用主线程. 主线程非常繁忙,因为它要处理绘制UI,响应用户的交互,默认情况下执行我们写下的大部分代码. 好的开发者知道他/她需要将重负荷的任务移除到工 ...

随机推荐

  1. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  2. centos 6.5安装VMware tools

    系统描述:win7旗舰版64位系统+VMware Workstation10+CentOS6.5(win7系统上安装了VMware Workstation10虚拟化软件,在该虚拟化软件上安装了Cent ...

  3. NBT(NetBIOS Over TCP)名称解析概述

    在微软IP网络中,客户计算机查找其他计算机并与之进行通信的主要手段是利用域名(DNS).但是,使用先前版本的Windows户机也使用NetBIOS协议,将名称解析为IP地址. 通过三种方法解析NetB ...

  4. JSON在PHP中的基本应用

    从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 该函数主要用来将数组和对象,转换为json格式 ...

  5. Maven项目META-INF文件夹不存在的问题

    直接在resources文件夹下新建META-INF文件夹即可. 参考: https://my.oschina.net/KingPan/blog/750159 https://stackoverflo ...

  6. 从零开始搭建linux下laravel 5.5所需环境(二)

    我们已经装好了nginx,现在我们开始装mysql和php 我们同样使用yum来安装. 先安装MySQL软件(客户端.服务器端.依赖库) yum install -y mysql mysql-serv ...

  7. java linux 项目常常无故被关闭 进程无故消息

    布了几个项目.竟然天天会自己主动的挂掉.急了.花时间攻克了一下.总结方案例如以下: 1.磁盘满了.这大家都懂,清一下 2.tomcat在关闭的或是重新启动的时候,经常后台进程没有被关闭.须要用ps a ...

  8. ARC 特性

    ResultVC *myResultVC = [[ResultVC alloc]initWithNibName:@"ResultVC" bundle:nil]; [self.vie ...

  9. oracle extract函数

    oracle Extract 函数 //oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分   //语法如下:   EXTRA ...

  10. 利用UIWebView获取userAgent需要注意的地方

    网络通信有时候需要传递参数userAgent,iOS中获取userAgent很简单. UIWebView* webView = [[UIWebView alloc] initWithFrame:CGR ...