Android主线程不能运行耗时操作。我们通常是在子线程中运行耗时操作,
我们在运行完耗时操作后,我们一般能够通过下面几种方式来实现ui界面的更新。 首先是布局文件:
<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" >
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp" />
<Button
android:id="@+id/update_mButton_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Hander Post"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Hander SendMessage"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="RunOnUiThread"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="View Post"
android:textSize="15sp" />
</LinearLayout>

————–代码实现,有凝视————————————-

public class MainActivity extends Activity implements OnClickListener {
private TextView mTextView;
private Button update_mButton_01;
private Button update_mButton_02;
private Button update_mButton_03;
private Button update_mButton_04;
private Handler mPostHander = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
// 更新UI
mTextView.setText("通过Hander Send Message更新Ui");
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initEvents();
} /**
* 初始化控件
* @description:
* @date 2015-10-8 上午10:55:49
*/
private void initViews() {
this.mTextView = (TextView) findViewById(R.id.mTextView);
this.update_mButton_01 = (Button) findViewById(R.id.update_mButton_01);
this.update_mButton_02 = (Button) findViewById(R.id.update_mButton_02);
this.update_mButton_03 = (Button) findViewById(R.id.update_mButton_03);
this.update_mButton_04 = (Button) findViewById(R.id.update_mButton_04);
} /**
* 事件监听
* @description:
* @date 2015-10-8 上午10:56:02
*/
private void initEvents() {
this.update_mButton_01.setOnClickListener(this);
this.update_mButton_02.setOnClickListener(this);
this.update_mButton_03.setOnClickListener(this);
this.update_mButton_04.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.update_mButton_01:// 第一种方式,通过Hander.post方法来实现UI更新
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒,模拟耗时操作
mPostHander.post(new Runnable() { @Override
public void run() {
// 更新UI
mTextView.setText("通过Hander Post更新Ui");
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
break;
case R.id.update_mButton_02:// 直接通过Hander发送Message来更新UI
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒。模拟耗时操作
mPostHander.sendEmptyMessage(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start(); break;
case R.id.update_mButton_03:// 通过runOnUiThread来实现ui更新
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒。模拟耗时操作
runOnUiThread(new Runnable() { @Override
public void run() {
// 更新UI
mTextView.setText("通过runOnUiThread更新Ui");
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start(); break;
case R.id.update_mButton_04:// 直接利用View.post方法来更新ui
mTextView.post(new Runnable() {
@Override
public void run() {
// 更新UI
mTextView.setText("通过View.post()更新Ui");
}
});
break;
}
}
}

Android学习笔记之:android更新ui的几种经常用法的更多相关文章

  1. [Android学习笔记]子线程更新UI线程方法之Handler

    关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...

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

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

  3. Android学习笔记--处理UI事件

    Handling UI Events 在Android里, 有不只一种方式可以截获用户与你的应用程序交互的事件. 在你的界面上处理事件时,你需要捕获用户与某个View实例交互时所产生的事件.View类 ...

  4. Android学习笔记(三) UI布局

    每一个布局都有其适合的方式,另外,这几个布局元素可以相互嵌套应用,做出美观的界面. 一.线性布局(LinearLayout) 线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下 ...

  5. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...

    下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...

  6. Android学习笔记之Android Studio添加新的Activity

    1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...

  7. Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns

    摘自:http://blog.csdn.net/sjf0115/article/details/7213565/ TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但 ...

  8. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  9. Android学习笔记之Android Studio下创建menu布局文件

    1.创建menu文件夹 Android Studio项目中如果没有menu文件夹,在res文件夹右键,new-Android resource directory: 则会弹出下图对话框,在Resour ...

随机推荐

  1. Codeforces Round 411 Div.2 题解

    A Fake NP standard input/output s, MB Submit Add to favourites x3673 B -palindrome standard input/ou ...

  2. 【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)

    自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下. 题目: 洛谷3181 分析: SAM(可能是)模板题(不会SAM的同学戳我:[知识总结]后缀自动机的构建). 对\(s1\)建出SAM,用\ ...

  3. ACM_迟到的祝福(四)

    迟到的祝福(四) Time Limit: 2000/1000ms (Java/Others) Problem Description: 据说前几天是雁来师姐的生日,作为一个15级的小鲜肉A,没及时给师 ...

  4. 【转】linux read 用法

    转自:http://www.cnblogs.com/iloveyoucc/archive/2012/04/16/2451328.html 1.基本读取 read命令接收标准输入(键盘)的输入,或其他文 ...

  5. MyBatis分页组件--PageHelper

    一.介绍 PageHelper是国内非常优秀的一款开源的 mybatis 分页插件,它支持基本主流与常用的数据库,例如 Oracle.Mysql.MariaDB.SQLite.Hsqldb 等. 官网 ...

  6. crontab的使用

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...

  7. Python--10、线程

    线程 每个进程里都有一个控制线程,进程不是一个执行单位,线程是执行单位,进程是资源单位(资源隔离).进程下可以开多个线程,多线程共享进程内的资源.创建线程的速度比创建进程的速度快,因为创建线程不需要再 ...

  8. drupal 8——打补丁(patch)

    druapl 的核心可能会有漏洞,这时就需要我们去打补丁.很多补丁都已经有人写好了,我这里讲的就是如何去打这些已经写好的补丁. 对于这个问题:drupal8 核心有bug导致了两个相同的错误提示的出现 ...

  9. 支付宝小程序日期选择组件datePicker封装

    github 地址 https://github.com/iocool/antminDatePicker 最近在做支付宝小程序(以下简称小程序)开发,发现小程序的日期选择组件很不好用,比如安卓和IOS ...

  10. Sql Server 优化 SQL 查询:如何写出高性能SQL语句

    1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来从一个 10万条记录的表中查1条 ...