博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 更新UI的两种方法——handler和runOnUiThread()
阅读量:3606 次
发布时间:2019-05-21

本文共 2185 字,大约阅读时间需要 7 分钟。

分类: 
 
459人阅读 
(0) 
 

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

在Android开发过程中,常需要更新界面的UI。而更新UI是要主线程来更新的,即UI线程更新。如果在主线线程之外的线程中直接更新页面显示常会报错。抛出异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)

话不多说,贴出下面的代码

方法一:

在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例, 在这个Handler实例的handleMessage回调函数中调用更新界面显示的函数。

界面:

[html] 
  1. <span style="font-size:14px;">public class MainActivity extends Activity {  
  2.     private EditText UITxt;  
  3.     private Button updateUIBtn;  
  4.     private UIHandler UIhandler;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         UITxt = (EditText)findViewById(R.id.ui_txt);  
  11.         updateUIBtn = (Button)findViewById(R.id.update_ui_btn);  
  12.         updateUIBtn.setOnClickListener(new View.OnClickListener() {  
  13.               
  14.             public void onClick(View v) {  
  15.                 // TODO Auto-generated method stub  
  16.                 UIhandler = new UIHandler();  
  17.                 UIThread thread = new UIThread();  
  18.                 thread.start();  
  19.             }  
  20.         });  
  21.     }  
  22.   
  23.     @Override  
  24.     public boolean onCreateOptionsMenu(Menu menu) {  
  25.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  26.         return true;  
  27.     }  
  28.     private class UIHandler extends Handler{  
  29.         @Override  
  30.         public void handleMessage(Message msg) {  
  31.             // TODO Auto-generated method stub  
  32.             super.handleMessage(msg);  
  33.             Bundle bundle = msg.getData();  
  34.             String color = bundle.getString("color");  
  35.             UITxt.setText(color);  
  36.         }  
  37.     }  
  38.     private class UIThread extends Thread{  
  39.         @Override  
  40.         public void run() {  
  41.             try {  
  42.                 Thread.sleep(3000);  
  43.             } catch (InterruptedException e) {  
  44.                 // TODO Auto-generated catch block  
  45.                 e.printStackTrace();  
  46.             }  
  47.             Message msg = new Message();  
  48.             Bundle bundle = new Bundle();  
  49.             bundle.putString("color", "黄色");  
  50.             msg.setData(bundle);  
  51.             MainActivity.this.UIhandler.sendMessage(msg);  
  52.               
  53.         }  
  54.     }  
  55. }</span>  
更新后:

方法二:利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。 这样Runnable对像就能在ui程序中被调用。如果当前线程是UI线程,那么行动是立即执行。如果当前线程不是UI线程,操作是发布到事件队列的UI线程

[html] 
  1. FusionField.currentActivity.runOnUiThread(new Runnable()    
  2.         {    
  3.             public void run()    
  4.             {    
  5.                 Toast.makeText(getApplicationContext(), , "Update My UI",    
  6.                         Toast.LENGTH_LONG).show();    
  7.             }    
  8.     
  9.         });    

转载地址:http://sixzn.baihongyu.com/

你可能感兴趣的文章
Linux-ssh服务及服务管理、文件传输
查看>>
Linux-网络配置
查看>>
开发中浏览器兼容的问题总结
查看>>
Vue初体验
查看>>
Vue学习之二(vue指令)
查看>>
人力资源项目-角色模块
查看>>
Matrixport首席执行官葛越晟:区块链市场具有充足的流动性及高溢价
查看>>
量子链创始人帅初:平台和应用需要具备区块链特征,但不一定需要去中心化...
查看>>
印度加密交易所解禁:交易量暴增6倍,全球Buy in了吗?
查看>>
明年3月实施!韩国通过特别金融法案,加密货币完全合法化
查看>>
7种启动Spring Boot项目的方式,一次性打包说给你听
查看>>
《Spring Cloud与Docker微服务架构实战》.pdf
查看>>
Vue表单上传带多张图片上传加预览,支持手机拍照上传
查看>>
Js动态生成Div、带属性。append()和appendChild()
查看>>
整合trtc遇到的坑:<ERROR> navigator.mediaDevices is undefined
查看>>
前端实现视频在线预览插件之video.js上手
查看>>
【Unity】删除所有子物体保留父物体的2种方式
查看>>
基本组件操作
查看>>
Time模块
查看>>
InputModule
查看>>