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] view plain copy
  1. <spanstyle="font-size:14px;">publicclassMainActivityextendsActivity{
  2. privateEditTextUITxt;
  3. privateButtonupdateUIBtn;
  4. privateUIHandlerUIhandler;
  5. @Override
  6. publicvoidonCreate(BundlesavedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. UITxt=(EditText)findViewById(R.id.ui_txt);
  10. updateUIBtn=(Button)findViewById(R.id.update_ui_btn);
  11. updateUIBtn.setOnClickListener(newView.OnClickListener(){
  12. publicvoidonClick(Viewv){
  13. //TODOAuto-generatedmethodstub
  14. UIhandler=newUIHandler();
  15. UIThreadthread=newUIThread();
  16. thread.start();
  17. }
  18. });
  19. }
  20. @Override
  21. publicbooleanonCreateOptionsMenu(Menumenu){
  22. getMenuInflater().inflate(R.menu.activity_main,menu);
  23. returntrue;
  24. }
  25. privateclassUIHandlerextendsHandler{
  26. @Override
  27. publicvoidhandleMessage(Messagemsg){
  28. //TODOAuto-generatedmethodstub
  29. super.handleMessage(msg);
  30. Bundlebundle=msg.getData();
  31. Stringcolor=bundle.getString("color");
  32. UITxt.setText(color);
  33. }
  34. }
  35. privateclassUIThreadextendsThread{
  36. @Override
  37. publicvoidrun(){
  38. try{
  39. Thread.sleep(3000);
  40. }catch(InterruptedExceptione){
  41. //TODOAuto-generatedcatchblock
  42. e.printStackTrace();
  43. }
  44. Messagemsg=newMessage();
  45. Bundlebundle=newBundle();
  46. bundle.putString("color","黄色");
  47. msg.setData(bundle);
  48. MainActivity.this.UIhandler.sendMessage(msg);
  49. }
  50. }
  51. }</span>
更新后:

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

[html] view plain copy
  1. FusionField.currentActivity.runOnUiThread(newRunnable()
  2. {
  3. publicvoidrun()
  4. {
  5. Toast.makeText(getApplicationContext(),,"UpdateMyUI",
  6. Toast.LENGTH_LONG).show();
  7. }
  8. });

更多相关文章

  1. Only the original thread that created a view hierarchy can t
  2. Android之adt 23找不到NDK路径设置解决方案
  3. Android界面布局基本属性
  4. Android新线程中更新主线程UI中的View方法汇总
  5. AOSP和Chromium的Android(安卓)WebViewTest
  6. Android(安卓)SDK下载和更新失败的解决方法!!!
  7. Android应用程序执行流程
  8. Android性能调优
  9. 更新Activity的几个方法

随机推荐

  1. Android 导入android源码有错,R.java文件
  2. Android使用DrawerLayout实现抽屉式侧滑
  3. Android(安卓)SdCard 新建文件夹并在文件
  4. 报错: Caused by: java.lang.RuntimeExce
  5. android的系统权限定义
  6. Android SearchView
  7. 如何学习Android应用程序的开发
  8. 关于android AppWidget初探
  9. android拍照上传
  10. Android实际开发中实用的第三方(开源)框架