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. [BZOJ4832]抵制克苏恩

    [BZOJ4832]抵制克苏恩 思路: \(f[i][j][k][l]\)表示打了\(i\)次,血量为\(1\sim 3\)的随从有\(j,k,l\)个的期望.转移时注意避免重复. 源代码: #inc ...

  2. hdu 5246 乱搞

    题意:题目太长直接看链接 链接:点我 乱搞题 显然,一个人要想成功,必须大于等于最强的人的战斗力,所以我们从后往前看 这里直接拿例1解释,首先递减排个序 15,13,10,9,8 作差得2,3,1,1 ...

  3. Python168的学习笔记5

    关于对csv文件的操作. python标准库中有csv的库,使用非常方便. import csv with open('pingan.csv','rb') as rf: reader = csv.re ...

  4. SGU 405 Totalizator

    405. Totalizator Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standardout ...

  5. java泛型中的E,K,V,T,U,S

    注释: java 泛型类型使用大写形式,且比较短,这是常见的 在java库中,使用变量 E 表示集合的元素类型 K 和 V 分别表示数据库表数据的键key和值value的类型 T(如果有需要还可以使用 ...

  6. 重温PHP之选择排序

    思路:一组数中,选出最小者与第一个位置数交换,然后在剩余数中再找最小者与第二个位置数交换,依次类推,循环到倒数第二个数和最后一个数比较为止. 测试代码: 结果:

  7. C#中的String与string

    在C#中,string 是 System.String 的别名,所以基本上在使用时是没有差别的. 习惯上,我们把字符串当作对象时(有值的对象实体),我们用string. 而我们把它当类时(需要字符串类 ...

  8. ubuntu 常用配置

    root 登录 sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf加:greeter-show-manual-login=true设 ...

  9. numpy转换

    csv2npy cccsv=numpy.genfromtxt('/root/c.csv', delimiter = ',') buf2npy imga=numpy.frombuffer(buf,num ...

  10. ubuntu下如何查看软件安装目录以及安装版本

    1)aptitude show 软件名 例如aptitude show kde-runtime 显示如下 ****@ubuntu:~$ aptitude show kde-runtime 软件包: k ...