How to change a TextView every second in Android

后端 未结 5 870
庸人自扰
庸人自扰 2020-12-10 02:52

I\'ve made a simple Android music player. I want to have a TextView that shows the current time in the song in minutes:seconds format. So the first thing I tried was to make

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 03:21

    You have to use a handler to handle the interaction with the GUI. Specifically a thread cannot touch ANYTHING on the main thread. You do something in a thread and if you NEED something to be changed in your main thread, then you call a handler and do it there.

    Specifically it would look something like this:

    Thread t = new Thread(new Runnable(){ ... do stuff here

    Handler.postMessage(); }

    Then somewhere else in your code, you do

    Handler h = new Handler(){

    something something... modify ui element here }

    Idea its like this, thread does something, notifies the handler, the handler then takes this message and does something like update a textview on the UI thread.

提交回复
热议问题