android进行异步更新UI的四种方式(转载)
2016-02-21 09:11
507 查看
android进行异步更新UI的四种方式
大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正:使用Handler消息传递机制;
使用AsyncTask异步任务;
使用runOnUiThread(action)方法;
使用Handler的post(Runnabel r)方法;
下面分别使用四种方式来更新一个TextView。
1.使用Handler消息传递机制
package com.example.runonuithreadtest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if(msg.what==0x123) { tv.setText("更新后的TextView"); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyThread().start(); } class MyThread extends Thread { @Override public void run() { //延迟两秒更新 try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendEmptyMessage(0x123); } } }
2. 使用AsyncTask异步任务
注:更新UI的操作只能在onPostExecute(String result)方法中。package com.example.runonuithreadtest; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new Yibu().execute(); } class Yibu extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub tv.setText("更新后的TextView"); } } }
3. 使用runOnUiThread(action)方法
package com.example.runonuithreadtest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyThread().start(); } class MyThread extends Thread { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { //延迟两秒更新 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } tv.setText("更新后的TextView"); } }); } } }
4. 使用Handler的post(Runnabel r)方法
全选<button href="javascript:void(0);" _xhe_href="javascript:void(0);" class="copyCode btn btn-xs" data-clipboard-text="" package="" com.example.runonuithreadtest;"="" data-toggle="tooltip" data-placement="top" title="" style="color: rgb(255, 255, 255); font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 12px; line-height: 1.5; font-family: inherit; margin: 0px 0px 0px 5px; overflow: visible; cursor: pointer; vertical-align: middle; border: 1px solid transparent; white-space: nowrap; padding-right: 5px; padding-left: 5px; border-radius: 3px; -webkit-user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px; background-image: none; background-color: rgba(0, 0, 0, 0.74902);">复制放进笔记package com.example.runonuithreadtest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); Handler handler = new Handler(); handler.post(new Runnable(){ @Override public void run() { try { //延迟两秒更新 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } tv.setText("更新后的TextView"); } }); } }
相关文章推荐
- UI_拖动View
- LeetCode Closest Binary Search Tree Value II
- 【POJ】[1679]The Unique MST
- LeetCode Closest Binary Search Tree Value
- UICollectionView自定义Layout之蜂窝布局
- reveal UI 分析工具简单使用 内附1.5.1破解版下载地址
- We Recommend a Singular Value Decomposition
- 【hdu1159】common subsequence —— 二维dp
- EasyUI闪屏EasyUI页面加载提示(原理+代码+效果图)
- String、StringBuffer与StringBuilder之间区别
- POJ 2533 —— Longest Ordered Subsequence 最长上升子序列裸题
- java中continue配合标签进行继续循环
- Handler、Looper和MessageQueue
- 为什么在jsp中写${pageContext.request.contextPath }失效了
- [置顶] NGUI另一种按钮事件触发的方法
- 1007. Maximum Subsequence Sum (25)
- Android Wear 进阶 - 3.1 Creating Custom UIs for Wear Devices-Defining Layouts 创建自定义的手表设备UIs-定义布局
- 【Codeforces#38G】Queue【Splay】【二分】
- [iOS]UIView中的坐标转换
- fixed fluid layout