MainActivity如下:

package cn.testlooper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.widget.TextView;
import android.widget.Toast;
/**
* Demo描述:
* 在子线程中Looper的使用
*
* 测试结果:
* 可在子线程中更改UI
*
* 原理备注:
* 在View和Toast的源码中均含有一个Handle
* 这样的话在子线程中:
* Handle Message Looper MessageQueue这套机制
* 就齐备了.
* 关于此原理有待于进一步研究
*
* 参考资料:
* http://blog.csdn.net/xiaanming/article/details/9344703
*/
public class MainActivity extends Activity {
public TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
} private void init() {
mTextView=(TextView) findViewById(R.id.textView);
testLooper();
} private void testLooper(){
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
mTextView.setText("9527");
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG).show();
Looper.loop();
}
}).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="@string/hello_world"
android:layout_centerInParent="true"
android:textSize="25sp"
/> </RelativeLayout>

Android利用Looper在子线程中改变UI的更多相关文章

  1. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  2. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  3. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  4. Android多线程之(一)View.post()源码分析——在子线程中更新UI

    提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...

  5. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  6. android UI 操作 不要在子线程中操作UI

    不管是android ,还是 ios ,请不要在子线程中操作UI,有时有些崩溃,从报错上看不出什么原因,就有可能是子线程操作了UI:切记,切记! 请放在主线程例: activity.runOnUiTh ...

  7. 如何在子线程中更新UI

    一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...

  8. 使用Handler在子线程中更新UI

    Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...

  9. C#子线程中更新ui

    本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...

随机推荐

  1. 《深入Java虚拟机学习笔记》- 第18章 finally子句

    本章主要介绍字节码实现的finally子句.包括相关指令以及这些指令的使用方式.此外,本章还介绍了Java源代码中finally子句所展示的一些令人惊讶的特性,并从字节码角度对这些特征进行了解释. 1 ...

  2. POJ 1904 King's Quest 强连通分量+二分图增广判定

    http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...

  3. CSLA.NET 简介

    CSLA.NET 据说在国外用的很多,国内介绍这个框架的文章目前网络上能找到的比较早,大多是早期的一些版本的版本的介绍.目前最新版的4.5.6 .版本的整体架构已经有了很大的变化.拟开一个系列,结合〈 ...

  4. javascript设计模式3

    门户大开式对象 var Book=function(isbn,title,author){ if (isbn==undefined) throw new Error("不合法"); ...

  5. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇02:内购如何实现》

    2.内购如何实现 内购概述: 游戏内购是指玩家在游戏中,用金钱获取游戏道具的一种方式.开发者从玩家内购之中获得开发游戏的收益,因此大量游戏都有内购模块.下面我们以在手机IOS平台实现跑酷游戏内购为例, ...

  6. Java之ByteArrayInputStream和ByteArrayOutputStream-操作字节数组的类

    ByteArrayInputStream和ByteArrayOutputStream 源:内存中的字节数组 目的:内存中的字节数组 这两个流对象不涉及底层资源的调用,操作的都是内存中的数组,所以不需要 ...

  7. quartus II 自动生成testbench

    如果自己不想写这些testbench的这些固定格式,可以在quartus里自动生成testbench文件的模板,然后往里面写信号就行了 步骤:processing->start->star ...

  8. nyoj 448 寻找最大数

    寻找最大数 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...

  9. getUTCHours

    getUTCMinutes() 方法可根据世界时 (UTC) 返回时间的分钟字段. function timeFormat(ms){ ; var date = new Date(ms), h = da ...

  10. java对身份证验证及正则表达式解析

    原文地址:http://www.cnblogs.com/zhongshengzhen/ java对身份证验证及正则表达式解析 package service; import java.text.Par ...