Android学习笔记之:android更新ui的几种经常用法
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的几种经常用法的更多相关文章
- [Android学习笔记]子线程更新UI线程方法之Handler
关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...
- Android 在子线程中更新UI的几种方法
第一种: new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { // 在这里 ...
- Android学习笔记--处理UI事件
Handling UI Events 在Android里, 有不只一种方式可以截获用户与你的应用程序交互的事件. 在你的界面上处理事件时,你需要捕获用户与某个View实例交互时所产生的事件.View类 ...
- Android学习笔记(三) UI布局
每一个布局都有其适合的方式,另外,这几个布局元素可以相互嵌套应用,做出美观的界面. 一.线性布局(LinearLayout) 线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下 ...
- Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...
下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...
- Android学习笔记之Android Studio添加新的Activity
1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...
- Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns
摘自:http://blog.csdn.net/sjf0115/article/details/7213565/ TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但 ...
- android学习笔记45——android的数据存储和IO
android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...
- Android学习笔记之Android Studio下创建menu布局文件
1.创建menu文件夹 Android Studio项目中如果没有menu文件夹,在res文件夹右键,new-Android resource directory: 则会弹出下图对话框,在Resour ...
随机推荐
- ACM_汉诺塔问题(递推dp)
Problem Description: 最近小G迷上了汉诺塔,他发现n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了 ...
- 337 House Robber III 打家劫舍 III
小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相 ...
- Product Device Lot
Product是指产品: 这个Product可以在不同的设备类型上生产, 同一类型的设备也可能硬件有差异,所以会有相对应的Device(Recipe): 同一Product(或同一Device)由于数 ...
- [Android]异常8-android.view.WindowManager$BadTokenException
背景:Service服务中使用WindowManager时,Android4.4使用正常,Android6.0使用应用崩溃停止运行,提示android.view.WindowManager$BadTo ...
- CSS——tab导航demo
问题总结: 1.ul要比外套div宽度的值大一点 2.ul需要往左移动1px 3.外套的div设置overflow隐藏 解决抖动: 1.li宽度设置98px,padding左右值1px,hover之后 ...
- IIS中实现http自动转换到https
IIS中实现http自动转换到https修改以下文件:C:\WINDOWS\Help\iisHelp\common\403-4.htm 为以下内容<!DOCTYPE HTML PUBLIC &q ...
- Centos 编译安装Haproxy
一.环境介绍 1.Centos6 2. haproxy-1.4.25.tar.gz 二.安装 $ curl -O http://haproxy.1wt.eu/download/1.4/src/hapr ...
- linux搭建mysql服务器及可视化工具
环境: ubutnu 18.4 mysql 5.7 参考: 安装 https://www.cnblogs.com/opsprobe/p/9126864.html 配置用户权限 https://baij ...
- jquery ajax中各个事件执行顺序如下
$(function(){ setTimeout(function(){ $.ajax({ url:'/php/selectStudent.php', }); },0); $(document).aj ...
- 惊了!!! 小白零基础学java (月薪过万是你的梦想嘛) 手把手教学 就怕你不动手【二十五】第二章【初识MySQL】
初识MySQL1. 了解主流的数据库和数据库分类1.1 数据库概念数据库:按照数据结构来组织.存储和管理数据的一种建立在计算机存储设备上的仓库. 数据库的优势: 1. 可以持久化存储大量的数据.方便我 ...