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

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

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

 

界面:

 

 

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);                    }    }}

 

更新后:

方法二:利用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();                  }                    });

 

<>

上面的写法让人迷惑:另外写一个实现Runable的类专门用于更新ui

private class UpdateUIFromRunnable implements Runnable{//to do your work......}

调用上面的:

runOnUiThread(new UpdateUIFromRunnable());

既可以实现更新UI了...

 

 

 

 

 

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

你可能感兴趣的文章
mysql sql 优化
查看>>
C++程序安装卸载WDM驱动
查看>>
Android签名总结
查看>>
在数据库中获取当前时间语句
查看>>
Oracle纯SQL实现递归查询分页(树查询分页)
查看>>
【定制化图像开放平台】入门实例之手写数字模型训练
查看>>
Kernel 内核线程
查看>>
360浏览器内核控制,配置使用极速模式或者兼容模式
查看>>
Win32窗口函数中获取鼠标位置
查看>>
批处理常用符号详解
查看>>
Ehcache的简单例子
查看>>
Oracle 操作中的问题
查看>>
LeetCode_150evalRPN [Evaluate Reverse Polish No..]
查看>>
informix分页
查看>>
sql 字符串替换
查看>>
UIWebView连接数
查看>>
html audio basic funcs
查看>>
2018给自己定个目标。
查看>>
Mac系统命令行修改host
查看>>
使用cookie实现跨域登录
查看>>