Pausing with handler and postDelayed in android

后端 未结 2 1091
傲寒
傲寒 2021-01-02 04:57

I\'m very new to android programming so please forgive my noobie-ness. I\'m trying to create a very simple activity that will have one TextView in the middle of the Layout a

2条回答
  •  时光取名叫无心
    2021-01-02 05:30

    postDelayed is non blocking, meaning it would add it to a queue of I'll do this later. So what you are probably seeing is all text updates happening together at the 7th second. I say this because you are postDelaying from the onCreate method when in reality you probably want to do it from onResume or even onPostResume.

    Also there is no reason to create a thread to add runnables to the post queue. Your code should look more like this: (Note the time to delay multiplier)

    @Override
    protected void onResume() {
        super.onResume();
        for (int count = 0; count < arraylength; count++){
            handler.postDelayed(new Runnable(){
                @Override
                public void run() {
                    mytexts.setText(myarray[count]);
                }
            }, 7000 * (count + 1));
        }
    }
    

提交回复
热议问题