Android 更新UI的两个方法
Android 更新UI的两个方法
在Android的开发过程中,常常需要适时的更新UI。Androd中的UI是在主线程中更新的。如果在主线程之外的线程中直接更新,就会出现报错并抛出异常:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)
那么Android中该如何更新UI呢?
<1>. 利用Activity.runOnUiThread(Runnable)把更新UI的代码写在Runnable中
操作机制:如果当前线程是UI线程,那么该行动立即执行;如果不是,操作发布到事件队列的UI线程。
RunOnUiThreadDemo
布局文件:activity_mai.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击更新界面"
/>
<TextView
android:id="@+id/input_str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout>
Java文件:MainActivity.java
package com.zsl.runonuithreaddemo; import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView viewText = (TextView)findViewById(R.id.input_str);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MainActivity.this.runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
char[] str = "Android is intresting...".toCharArray();
viewText.setText(str, 0, str.length);
}
}); }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
运行结果:
点击Button之前: 点击Button之后:
<2>. 在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例,利用Handler的回调实现。
事实上,我从这里的一篇英文文章中了解到,Android的handler中也是通过runOnUiThread实现的。
Android 更新UI的两个方法的更多相关文章
- 我的Android最佳实践之—— Android更新UI的两种方法:handler与runOnUiThread()
在Android开发过程中,常需要更新界面的UI.而更新UI是要主线程来更新的,即UI线程更新.如果在主线线程之外的线程中直接更新页面 显示常会报错.抛出异常:android.view.ViewRoo ...
- Android更新UI的两种方法——handler与runOnUiThread()
在Android开发过程中,常需要更新界面的UI.而更新UI是要主线程来更新的,即UI线程更新.如果在主线线程之外的线程中直接更新页面 显示常会报错.抛出异常:android.view.ViewRoo ...
- Android 更新UI的两种方法——handler和runOnUiThread()
今天看到了一个runOnUiThread()方法用来更新UI,觉得很神奇!! 方法一:handler机制不说了. 方法二:利用Activity.runOnUiThread(Runnable)把更新ui ...
- 转:探讨android更新UI的几种方法
本文转自:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...
- 【转】探讨android更新UI的几种方法----不错
原文网址:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...
- 探讨android更新UI的几种方法
作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...
- 探讨android更新UI的几种方法(转)
作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...
- Android更新UI的几种方法
在Android开发过程中,常需要更新界面的UI.比如网络请求操作.一些耗时操作都不能放在UI线程中运行的,需要放在子线程,而子线程又不能更新UI界面,这是我们需要引入一个Handler,消息处理机制 ...
- iOS子线程更新UI的两种方法
http://blog.csdn.net/libaineu2004/article/details/45368427 方法1:performSelectorOnMainThread[self perf ...
随机推荐
- [转载]hadoop SecondNamenode详解
SecondNamenode名字看起来很象是对第二个Namenode,要么与Namenode一样同时对外提供服务,要么相当于Namenode的HA.真正的了解了SecondNamenode以后,才发现 ...
- 从SQL2008R2导入数据到SQL2005
从SQL2008R2导入数据到SQL2005,数据很大,数据文件大概有120G. 尝试过直接离线附加,失败 尝试过备份还原,失败 最后找到了 1.先执行 exec sp_msforeachtable ...
- AspNet MVC : 操作/控制器过滤器(action filter)
1.Action Filter Action Filter提供了在执行action/controller前后对请求/响应修改的能力,可以应用于action和控制器上,作用在控制器上将自动被应用到该控制 ...
- POJ1007-DNA Sorting-ACM
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 83442 Accepted: 33584 Des ...
- Python - 多元组(tuple)
声明一个多元组 (4, 5, 6) 这是列表 [4, 5, 6] 与列表不一样在于多元组使用() 来组织元素而list使用方括号[] 而且多元组不能更改,用于当你的数组不想像list一样会被更改时就使 ...
- UBUNTU下FPT工具--lftp使用说明
lftp 是UBUNTU下一个功能强大的下载工具,它支持访问文件的协议: ftp, ftps, http, https, hftp, fish.(其中ftps 和https需要在编译的时候包含open ...
- Delphi 的各种错误信息(中英文)
******************************* * 编 译 错 误 信 息 * ******************************* ';' not allowed befo ...
- struts2.0 struts.xml配置文件详解
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quo ...
- Hibernate 配置详解(2)
6) hibernate.session_factory_name: 配置一个JNDI名称,通过Configuration对象创建的SessionFactory会绑定到JNDI下该名称中.一般名字格式 ...
- 1027. Colors in Mars (20) PAT
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1027 简单题,考察十进制数和n进制数的转换和输出格式的控制. People in Mars rep ...