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回调函数中调用更新界面显示的函数。


界面:

Android 更新UI的两种方法——handler和runOnUiThread()_第1张图片

public class MainActivity extends Activity {private EditText UITxt;private Button updateUIBtn;private UIHandler UIhandler;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        UITxt = (EditText)findViewById(R.id.ui_txt);        updateUIBtn = (Button)findViewById(R.id.update_ui_btn);        updateUIBtn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubUIhandler = new UIHandler();UIThread thread = new UIThread();thread.start();}});    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    private class UIHandler extends Handler{    @Override    public void handleMessage(Message msg) {    // TODO Auto-generated method stub    super.handleMessage(msg);    Bundle bundle = msg.getData();    String color = bundle.getString("color");    UITxt.setText(color);    }    }    private class UIThread extends Thread{    @Override    public void run() {    try {Thread.sleep(3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}    Message msg = new Message();    Bundle bundle = new Bundle();    bundle.putString("color", "黄色");    msg.setData(bundle);    MainActivity.this.UIhandler.sendMessage(msg);        }    }}
更新后:

Android 更新UI的两种方法——handler和runOnUiThread()_第2张图片

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

    FusionField.currentActivity.runOnUiThread(new Runnable()              {                  public void run()                  {                      Toast.makeText(getApplicationContext(), , "Update My UI",                              Toast.LENGTH_LONG).show();                  }                    });  



更多相关文章

  1. Android/java 多线程(五)-ThreadPoolExecutor线程池的使用
  2. Android 线程池来管理线程
  3. Android的线程使用来更新UI----Thread、Handler、Looper、TimerT
  4. Android关于线程更新UI的方法
  5. Android笔记 - 常见错误解决方法 | Android common erros and so
  6. Android,UI主线程与子线程
  7. 解析Android中的main线程与子线程

随机推荐

  1. Android(安卓)应用崩溃后重启的机制
  2. Android培训班(60)dex文件格式
  3. 学习android内核 -- 内存管理相关
  4. Android联系人4--联系人查询
  5. Android(安卓)@+id与@id的区别
  6. Android(安卓)Pull方式解析XML
  7. Android中 Js 扩展及交互
  8. 利用Hierarchy Viewer优化布局 ---》andr
  9. Android核心分析之一:分析方法论探讨之设
  10. android用户界面-对话框