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

时间:2021-12-31 23:19:10

MainActivity如下:

package cc.testui1;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 在子线程中更改UI的方式一
*
* 在子线程中利用主线程的Handler的post()方法
* 更改UI这个在子线程中sendMessage()原理和
* 形式都很类似.
*
* 参考资料:
* http://blog.csdn.net/guolin_blog/article/details/9991569
* Thank you very much
*/ public class MainActivity extends Activity {
private TextView mTextView;
private Handler mHandler;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mHandler=new Handler();
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() {
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText("My 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试在子线程中更新UI"
android:layout_centerInParent="true"
/> </RelativeLayout>

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

  1. Android在子线程中更新UI&lpar;二&rpar;

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

  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&period;post&lpar;&rpar;源码分析——在子线程中更新UI

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

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

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

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

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

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

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

  9. C&num;子线程中更新ui

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

随机推荐

  1. Java 第五章 循环结构1

    循环结构 1 while 循环结构 ,do- while 循环结构 . 循环结构: 必须满足两个条件 . 1,循环条件 和 循环 操作 ! while 循环 特点:先判断,再执行 , 编码规范:缩进, ...

  2. Protues记录文档&lowbar;1

    1.实现器件的连线端有红蓝小方块来表示改端电平变化. 操作:选菜单:SYSTEM\SET ANIMATION OPTION ,相应的勾选对应的选项2 附:可以添加逻辑探测器,在库中直接搜索“LOGIC ...

  3. Docker Resources

    Menu Main Resources Books Websites Documents Archives Community Blogs Personal Blogs Videos Related ...

  4. Phonegap 版本minSdkVersion为8的时候的自动更新与升级

    清单文件中: <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/> ...

  5. Spring3&period;0学习笔记文档的官方网站(六)--3&period;4&period;1

    3.4 依靠 3.4.1 依赖注入     依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI     参数是引进一个对象的.和缺乏父母之前-子类关系: ...

  6. Qt窗体引用window自带阴影边框效果

    <1>.工程pro文件添加Dwmapi.lib LIBS += Dwmapi.lib <2>.窗体控件添加系统函数 #ifdef Q_OS_WIN #include <D ...

  7. APP产品设计及运营时常见的问题

    目录 一.APP设计之初必须预埋的功能 二.H5活动页入口设计 三.全套icon /menu icon / logo图,统一后台设置便于活动推广 四.webview与原生页面的路由应统一改善用户体验 ...

  8. 用pigz代替gzip -- 并行压缩软件

    用pigz代替gzip By yejr on 03 十二月 2012 pig是个啥东东?官网:http://zlib.net/pigz一句话简介: A parallel implementation ...

  9. 反卷积Deconvolution

    反卷积(转置卷积.空洞卷积(微步卷积))近几年用得较多,本篇博客主要是介绍一下反卷积,尤其是怎么计算反卷积(选择反卷积的相关参数) 图1 空洞卷积(微步卷积)的例子,其中下面的图是输入,上面的图是输出 ...

  10. 「Python」人脸识别应用

    人脸识别主要步骤: face_recognition 库的安装 安装此库,首先需要安装编译dlib,此处我们偷个懒,安装软件Anaconda(大牛绕过),此软件预装了dlib. 安装好后,我们直接通过 ...