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()方法.
* 依据源码可知它在最终还是调用了handler的post()方法.
* 这个handler是主线程一个与View紧密相关的handler
*
* 参考资料:
* 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());
} 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");
}
});
}
}
}

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>

Android在子线程中更新UI(二)的更多相关文章

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

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

  2. Android 在子线程中更新UI

    今天在做练习时,在一个新开启的线程中调用“Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();” ...

  3. Android 在子线程中更新UI的几种方法

    第一种: new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { // 在这里 ...

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

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

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

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

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

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

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

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

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

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

  9. C#子线程中更新ui

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

随机推荐

  1. S3C3440看门狗驱动程序

    S3C3440看门狗驱动程序 http://www.cnblogs.com/lfsblack/archive/2012/09/13/2684079.html 看门狗是当CPU进入错误状态后,无法恢复的 ...

  2. 强化一下开源库:Synopse

    http://synopse.info/fossil/wiki/Synopse+OpenSource 有空要研究一下,只有写在这里,才会时时刻刻提醒自己.

  3. 单击Android设备后退键,主屏幕键以及旋转屏幕如何影响Activity的生命周期

    单击设备的后退键,相当于通知Android系统“我已完成activity的使用,现在不需要它了.”接到指令后,系统立即销毁了activity.即调用onPause()->onStop()-> ...

  4. 14.10.1 InnoDB Disk I/O

    14.10 InnoDB Disk IO and File Space Management InnoDB 磁盘IO和文件空间管理: 14.10.1 InnoDB Disk I/O 14.10.2 F ...

  5. 根据input 标签取value属性的值

    jrhmpt01:/root/lwp/0526# cat a1.pl use LWP::UserAgent; use DBI; use POSIX; use Data::Dumper; use HTM ...

  6. 基本HTML5文件结构

    作者 : Dolphin 原文地址:http://blog.csdn.net/qingdujun/article/details/28129039 基本HTML5文件结构: <!--<!D ...

  7. C# 数据访问编码需要遵循的几个规范

    一,链接打开之后必须关闭,否则会占用系统空间 SqlConnection conn=new SqlConnection(CONNECTIONSTRING); conn.open(); conn.clo ...

  8. JS - 侧边导航收缩伸展

    下载地址:http://www.lanrentuku.com/js/daohang-1162.html

  9. mysql 创建函数set global log_bin_trust_function_creators=TRUE;

    <pre name="code" class="html">set global log_bin_trust_function_creators=T ...

  10. MapReduce调度与执行原理之作业初始化

    前言 :本文旨在理清在Hadoop中一个MapReduce作业(Job)在提交到框架后的整个生命周期过程,权作总结和日后参考,如有问题,请不吝赐教.本文不涉及Hadoop的架构设计,如有兴趣请参考相关 ...