Update the UI outside the main thread

后端 未结 5 1157
梦谈多话
梦谈多话 2021-01-17 01:21

I am totaly new to android and just want to know if it is any working and possible way to update the UI outside the main thread. Just from my code I have listed below I know

5条回答
  •  不知归路
    2021-01-17 02:07

    A worker thread never can update main thread because only main thread can render the UI elements (TextView, EditText etc.) and if we try to update for sure we are going to an exception.

    myAcitity.runOnUiThread(new Runnable() {
        public void run() {
            //here you can update your UI elements because this code is going to executed by main thread
        }
    });
    

    Otherwise you can use post method of View class

    myView.post(new Runnable() {
            public void run() {
                //here you can update your UI elements because this code is going to executed by main thread
            }
        });
    

提交回复
热议问题