1. Android主线程不能运行耗时操作。我们通常是在子线程中运行耗时操作,
  2. 我们在运行完耗时操作后,我们一般能够通过下面几种方式来实现ui界面的更新。
  3. 首先是布局文件:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/mTextView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:textSize="16sp" />
  12. <Button
  13. android:id="@+id/update_mButton_01"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="10dp"
  17. android:text="Hander Post"
  18. android:textSize="15sp" />
  19. <Button
  20. android:id="@+id/update_mButton_02"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_margin="10dp"
  24. android:text="Hander SendMessage"
  25. android:textSize="15sp" />
  26. <Button
  27. android:id="@+id/update_mButton_03"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_margin="10dp"
  31. android:text="RunOnUiThread"
  32. android:textSize="15sp" />
  33. <Button
  34. android:id="@+id/update_mButton_04"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_margin="10dp"
  38. android:text="View Post"
  39. android:textSize="15sp" />
  40. </LinearLayout>

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

  1. public class MainActivity extends Activity implements OnClickListener {
  2. private TextView mTextView;
  3. private Button update_mButton_01;
  4. private Button update_mButton_02;
  5. private Button update_mButton_03;
  6. private Button update_mButton_04;
  7. private Handler mPostHander = new Handler() {
  8. public void handleMessage(android.os.Message msg) {
  9. if (msg.what == 1) {
  10. // 更新UI
  11. mTextView.setText("通过Hander Send Message更新Ui");
  12. }
  13. };
  14. };
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. initViews();
  20. initEvents();
  21. }
  22. /**
  23. * 初始化控件
  24. * @description:
  25. * @date 2015-10-8 上午10:55:49
  26. */
  27. private void initViews() {
  28. this.mTextView = (TextView) findViewById(R.id.mTextView);
  29. this.update_mButton_01 = (Button) findViewById(R.id.update_mButton_01);
  30. this.update_mButton_02 = (Button) findViewById(R.id.update_mButton_02);
  31. this.update_mButton_03 = (Button) findViewById(R.id.update_mButton_03);
  32. this.update_mButton_04 = (Button) findViewById(R.id.update_mButton_04);
  33. }
  34. /**
  35. * 事件监听
  36. * @description:
  37. * @date 2015-10-8 上午10:56:02
  38. */
  39. private void initEvents() {
  40. this.update_mButton_01.setOnClickListener(this);
  41. this.update_mButton_02.setOnClickListener(this);
  42. this.update_mButton_03.setOnClickListener(this);
  43. this.update_mButton_04.setOnClickListener(this);
  44. }
  45. @Override
  46. public void onClick(View view) {
  47. switch (view.getId()) {
  48. case R.id.update_mButton_01:// 第一种方式,通过Hander.post方法来实现UI更新
  49. new Thread() {
  50. public void run() {
  51. try {
  52. sleep(2000);// 休眠2秒,模拟耗时操作
  53. mPostHander.post(new Runnable() {
  54. @Override
  55. public void run() {
  56. // 更新UI
  57. mTextView.setText("通过Hander Post更新Ui");
  58. }
  59. });
  60. }
  61. catch (InterruptedException e) {
  62. e.printStackTrace();
  63. }
  64. };
  65. }.start();
  66. break;
  67. case R.id.update_mButton_02:// 直接通过Hander发送Message来更新UI
  68. new Thread() {
  69. public void run() {
  70. try {
  71. sleep(2000);// 休眠2秒。模拟耗时操作
  72. mPostHander.sendEmptyMessage(1);
  73. }
  74. catch (InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. };
  78. }.start();
  79. break;
  80. case R.id.update_mButton_03:// 通过runOnUiThread来实现ui更新
  81. new Thread() {
  82. public void run() {
  83. try {
  84. sleep(2000);// 休眠2秒。模拟耗时操作
  85. runOnUiThread(new Runnable() {
  86. @Override
  87. public void run() {
  88. // 更新UI
  89. mTextView.setText("通过runOnUiThread更新Ui");
  90. }
  91. });
  92. }
  93. catch (InterruptedException e) {
  94. e.printStackTrace();
  95. }
  96. };
  97. }.start();
  98. break;
  99. case R.id.update_mButton_04:// 直接利用View.post方法来更新ui
  100. mTextView.post(new Runnable() {
  101. @Override
  102. public void run() {
  103. // 更新UI
  104. mTextView.setText("通过View.post()更新Ui");
  105. }
  106. });
  107. break;
  108. }
  109. }
  110. }

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. [转]linux 下 join命令总结

    转自:http://blog.chinaunix.net/uid-20754793-id-177777.html 有两个文件需要合并,开始写了脚本实现,忽然发现join命令能够完全替代,总结了一下jo ...

  2. 使用Jupter Notebook实现简单的神经网络

    参考:http://python.jobbole.com/82208/ 注:1)# %matplotlib inline 注解可以使Jupyter中显示图片 2)注意包的导入方式 一.使用的Pytho ...

  3. CSS——img标签消除3px

    1.dispaly:block 2.float:left 这两种都可以消除3px

  4. SQL基本操作——存储过程

    存储过程类似于C#中的方法. --创建存储过程 create proc usp_TwoNumberAdd @num1 int, @num2 int as begin select @num1+@num ...

  5. 使用jQuery的toggle()方法对HTML标签进行显示、隐藏操作

    这是一个示例: <html> <head> <script type="text/javascript" src="https://code ...

  6. tidyverse生态链

    一套完整的数据分析流程 , 如下图所示 从图中可以看到,整个流程包括读取数据,整洁数据,数据探索和交流部分.经过前两部分, 我们可以得到一个整理好的数据,它的每一行都是一个样本 , 每一列是一个变量. ...

  7. CSS 之自定义滚动条样式

    ::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1px; } ::-webkit-scrollbar-t ...

  8. 解决Mysql Workbench的Error Code: 1175错误

    错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...

  9. Android 双屏异显

    android双屏是克隆模式,如果要在第二屏幕显示不同内容,需要自定义一个Presentation类 1.先设置权限 (刚开始折腾很久没有效果,后来发现是没设置权限) <!-- 显示系统窗口权限 ...

  10. JTable设置表格背景颜色——隔行不同

    package view; import java.awt.Color; import java.awt.Component; import javax.swing.JLabel; import ja ...