Android更新UI的五种方式:

1.handler.post

2.activity.runOnUiThread

3.view.post

4.handler+Thread

5.AsyncTask

下面来看下以下的例子

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{private Layout layout;private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initLayout();initValue();}private void initLayout(){layout=new Layout();layout.helloText=(TextView) findViewById(R.id.helloText);layout.webImg=(ImageView) findViewById(R.id.webImg);layout.button1=(Button) findViewById(R.id.button1);layout.button2=(Button) findViewById(R.id.button2);layout.button3=(Button) findViewById(R.id.button3);layout.button4=(Button) findViewById(R.id.button4);layout.button5=(Button) findViewById(R.id.button5);layout.button1.setOnClickListener(this);layout.button2.setOnClickListener(this);layout.button3.setOnClickListener(this);layout.button4.setOnClickListener(this);layout.button5.setOnClickListener(this);}private void initValue(){layout.button1.setText("1.Handler.post");layout.button2.setText("2.activity.runOnUiThread");layout.button3.setText("3.view.post");layout.button4.setText("4.handler+Thread");layout.button5.setText("5.AsyncTask");}private class Layout{TextView helloText;ImageView webImg;Button button1;Button button2;Button button3;Button button4;Button button5;}//1.Handler.postprivate void method_1(){new Handler().postDelayed(new Runnable() {@Overridepublic void run() {layout.helloText.setText("Handler的postDelayed延时一秒");}}, 1000);}//2.activity.runOnUiThreadprivate void method_2(){new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);runOnUiThread(new Runnable() {@Overridepublic void run() {layout.helloText.setText("在子线程中使用activity.runOnUiThread");}});} catch (InterruptedException e) {e.printStackTrace();}}}).start();}//3.view.postprivate void method_3(){new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);layout.helloText.post(new Runnable() {@Overridepublic void run() {layout.helloText.setText("在子线程中使用TextView.post");}});} catch (InterruptedException e) {e.printStackTrace();}}}).start();}//4.Handler+Threadprivate void method_4(){handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if(msg.what==1){Bundle bundle=msg.getData();String name=bundle.getString("name");String sex=bundle.getString("sex");String age=bundle.getString("age");layout.helloText.setText(name+"\n"+sex+"\n"+age);}}};Thread thread=new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Message msg=Message.obtain();msg.what=1;Bundle bundle=new Bundle();bundle.putString("name", "张三");bundle.putString("sex", "男");bundle.putString("age", "16");msg.setData(bundle);handler.sendMessage(msg);}});thread.start();}//5.AsyncTaskprivate void method_5(){        AsyncTask<String, Integer, Bitmap> async=new AsyncTask<String, Integer, Bitmap>(){@Overrideprotected Bitmap doInBackground(String... params) {publishProgress(0);HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(params[0]);publishProgress(30);final Bitmap bitmap;try {HttpResponse response = client.execute(get);bitmap = BitmapFactory.decodeStream(response.getEntity().getContent());} catch (Exception e) {return null;}publishProgress(100);return bitmap;}@Overrideprotected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);layout.helloText.setText("进度-"+values[0]+"%");}@Overrideprotected void onPostExecute(Bitmap result) {super.onPostExecute(result);if(result==null){layout.helloText.setText("获取图片失败");}else{layout.helloText.setText("获取图片成功");layout.webImg.setImageBitmap(result);}}@Overrideprotected void onPreExecute() {layout.webImg.setImageBitmap(null);}};        async.execute("http://imgsize.ph.126.net/?imgurl=http://img2.ph.126.net/-RloZHuMn-ultOW_YDF5fQ==/" + "6608648723609095427.jpg_96x96x0x90.jpg");        }@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.button1:method_1();break;case R.id.button2:method_2();break;case R.id.button3:method_3();break;case R.id.button4:method_4();break;case R.id.button5:method_5();break;default:break;}}
}

[Android开发]Android更新UI的五种方式相关推荐

  1. android ui 最新教程,Android更新UI的五种方式,androidui五种

    Android更新UI的五种方式,androidui五种handler.post activity.runOnUiThread view.post handler+Thread AsyncTask 例 ...

  2. Android 更新UI的几种方式

    1.Activity的 runOnUiThread textView = (TextView) findViewById( R.id.tv ); new Thread(new Runnable() { ...

  3. Android开发:操作UI线程4种方法

    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...

  4. AndroidStudio子线程更新UI的几种方式

    在安卓开发中,大部分情况下是不能在子线程直接更新UI的,只能在UI线程更新UI,其根本原因在于加入在一个Activity中有多个线程去更新UI,且没有加锁机制,可能会产生界面混乱的情况,但是如果都加锁 ...

  5. AndroidUI——后台线程更新UI的几种方式

    开发Android程序时经常会用到后台线程请求服务器数据,当请求完数据后更新UI时,经常遇到回调函数中和UI相关的语句无法执行,甚至有时候会抛异常. 下面的例子我会讲到三种方法更新UI,例子非常简单, ...

  6. java 更新ui_你怎么解决Android开发中更新UI报错的异常吗-百度经验

    执行上述代码. 在App中点击"变透明"的按钮后,App就退出了. logcat中打印了这个错: 03-20 14:47:31.326 11640-11796/com.exampl ...

  7. [UE4]更新UI的三种方式

    一.函数绑定 二.属性绑定 只会列出匹配的数据类型. 三.事件驱动更新 啦啦啦啦啦 结论:函数和属性绑定的原理都是每帧都去调用绑定的函数/属性,效率比较低下,一般不推荐使用.事件驱动更新的效率最好,性 ...

  8. Android 更新UI的两种方法——handler和runOnUiThread()

    Android 更新UI的两种方法--handler和runOnUiThread() 在Android开发过程中,常需要更新界面的UI.而更新UI是要主线程来更新的,即UI线程更新.如果在主线线程之外 ...

  9. Android数据存储五种方式总结

    1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用ContentProvider存储数据 5 网络存储数据 下面详细讲解 ...

最新文章

  1. Visual Studio 2019更新到16.2.2
  2. idea 往 Github 上 push 失败
  3. 隐藏的东西? 您需要HiddenSidesPane
  4. 网管交换机怎么设置?网管交换机设置方法
  5. HTML如何添加锚点,干货满满
  6. python 正则表达式简介
  7. Ubuntu编写第一个Python程序
  8. 六个主要的社会网络分析软件的比较
  9. 移动通信-多径效应,多普勒效应,菲涅尔区,阴影效应,快衰落,慢衰落
  10. 数据分析~中国五大城市PM2.5数据分析02
  11. 世界名牌大学公开课集
  12. 网上出售企业支付宝骗局,不看后悔
  13. 如何获取安卓手机设备名称(包括用户自定义名称)
  14. 项目整合微信扫码登录功能
  15. 7个最好的文件存储网站
  16. selenium+chrome使用webrtc音频或视频时,默认开启麦克风和摄像头
  17. 画出漂亮的神经网络图,神经网络可视化工具集锦
  18. 高效办公之高效学习技巧:倍速学习软件详解
  19. Linux查看进程信息相关(Ubuntu)
  20. php用打印出实心菱形,php打印实心菱形

热门文章

  1. 多微源储能系统调频控制matlab/simulink模型
  2. mysql 手机类型_mysql 手机号存储类型
  3. 3dmax人头建模,人物头像建模3dmax人头建模,人物头像建模的详细图文教程来了,实用性非常强的详细图文教程来了,实用性非常强
  4. 【AI大咖】认真认识一代AI教父Hinton
  5. win10电脑桌面透明便签_在win10系统的电脑桌面上使用便签贴方法!
  6. HTML中table表格拆分合拼(colspan、rowspan)
  7. LDAP账号的统一管理
  8. 如何规避企业财务风险
  9. Docker 学习笔记-08:Dockerfile
  10. 试试这些智能抠图一键去背景软件