• 关于runOnUiThread()与Handler两种更新UI的方法


    在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]  
    <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);  
                  
            }  
        }  
    }
     
    方法二:利用Activity.runOnUiThread(Runnable)把更新UI的代码创建到Runnable中,然后在需要更新UI时,把这个Runnable对象传递给Activity.runOnUiThread(Runnable)。这样Runnable对象就能在UI程序中被调用。如有当前线程是UI 线程,那么行动是立即执行。如果当前线程不是UI线程,操作是发布到实现队列的UI线程中。
    FusionField.currentActivity.runOnUiThread(new Runnable(){
    public void run(){//方法执行体}})
     
  • 相关阅读:
    tensorboard使用命令
    FFmpeg安装以及视频转成图片_图片转成视频_以及分辨率转换
    yolo3各部分代码详解(超详细)
    OpenCV+python 彩色图像通道拆分与组合并判断size,shape函数的用法与区别
    基于Keras 的VGG16神经网络模型的Mnist数据集识别并使用GPU加速
    以Mnist为例从头开始自己建立数据集,搭建resnet34,识别Mnist
    2017年Brats 脑肿瘤挑战赛Automatic Brain Tumor Segmentation using Cascaded Anisotropic Convolutional Neural Networks
    2018年BRATS 肿瘤分割挑战赛第三名分割方案One-pass Multi-task Networks with Cross-task Guided Attention for Brain Tumor Segmentation
    脑胶质瘤论文笔记
    .npy文件的保存与加载
  • 原文地址:https://www.cnblogs.com/hun2014/p/5758988.html
Copyright © 2020-2023  润新知