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 {@Overrideprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubchar[] str = "Android is intresting...".toCharArray();viewText.setText(str, 0, str.length);}});}});}@Overridepublic 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之后:

Android 更新UI的两个方法_第1张图片


<2>. 在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例,利用Handler的回调实现。

具体参见:Android Handler消息传递机制

事实上,我从这里的一篇英文文章中了解到,Android的handler中也是通过runOnUiThread实现的。


更多相关文章

  1. android中的线程池
  2. Android异步更新UI-线程池-Future-Handler实例分析
  3. Android 进程和线程模型
  4. Android Handler 线程消息机制
  5. Android——进程与线程
  6. [Android] Android进程和线程模型
  7. android 自定义线程池ThreadPoolUtils工具类
  8. Android进程与线程基本知识四

随机推荐

  1. php如何解析url?解析url的5种方式介绍
  2. PHP如何实现AES加密、解密?方法介绍(代码示
  3. PHP如何解压缩zip文件?(代码示例)
  4. PHP 进程管理器 PHP-FPM
  5. PHP如何使用mpdf将html页面转换pdf文件?
  6. php大神进阶之try catch
  7. php保留两位小数的几种方法介绍
  8. 想学php编程?那你必看这些书
  9. 一分钟带你了解php和Python的区别
  10. 谈谈​PHP防止XSS跨站脚本攻击的方法