Android 更新UI的两种方法——handler和runOnUiThread()
来源:互联网 发布:mac地址过滤是什么意思 编辑:程序博客网 时间:2024/05/29 11:00
在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 plaincopy
- <span style="font-size:14px;">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 stub
- UIhandler = 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 block
- e.printStackTrace();
- }
- Message msg = new Message();
- Bundle bundle = new Bundle();
- bundle.putString("color", "黄色");
- msg.setData(bundle);
- MainActivity.this.UIhandler.sendMessage(msg);
- }
- }
- }</span>
更新后:
方法二:利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。 这样Runnable对像就能在ui程序中被调用。如果当前线程是UI线程,那么行动是立即执行。如果当前线程不是UI线程,操作是发布到事件队列的UI线程
[html] view plaincopy
- FusionField.currentActivity.runOnUiThread(new Runnable()
- {
- public void run()
- {
- Toast.makeText(getApplicationContext(), , "Update My UI",
- Toast.LENGTH_LONG).show();
- }
- });
2 0
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android 更新UI的两种方法——handler和runOnUiThread()
- Android中更新UI的两种方法——handler和runOnUiThread()
- archlinux xfce4 l2tp服务启动失败
- CQOI2016 总结
- Oracle11g 新特性 虚拟列
- 【剑指offer系列】 把数组排成最小的数___33
- bug记录1
- Android 更新UI的两种方法——handler和runOnUiThread()
- Jmeter 结合 Servers Performance Monitoring 插件进行性能监控
- leetcode 189. Rotate Array
- 在终端输入多行信息并且查找需要的信息且打印出含有该信息的行(不使用库函数)
- python+caffe做图片处理可能会用到的方法
- Out.flush用法
- TCP协议三次握手连接四次握手断开和DOS攻击
- 【计算几何各种知识点总结】[不定期补充]
- Ajax、jquery实现前后台数据交互