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之后:



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

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

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


更多相关文章

  1. Android(安卓)Tcp服务器端
  2. 多线程例子 android camera capture
  3. android SQLite封装类
  4. 垂直滚动公告栏
  5. Android学习笔记(7)————Android中的消息机制
  6. android 子线程handler 实现
  7. android 子线程handler 实现
  8. Android(安卓)之 Handler的分析
  9. Android(安卓)安卓使用MediaPlayer播放列表视频

随机推荐

  1. 项目练习总结
  2. 从阿里云故障说 Io hang 是什么?
  3. JavaMySQL面试题,如何书写 update 避免表
  4. 监控系统项目实施--安装与部署--添加一台
  5. 关于极客时间活动的中奖公告
  6. 线程面试必备:线程状态和dump输出状态,文末
  7. 面试必备,电商系统中并发测试是怎样进行的
  8. MySQL:表级锁、行级锁、共享锁、排他锁、
  9. prometheus + granfana
  10. 使用Glue抓取Azure SQL Database的数据库