How do we use runOnUiThread in Android?

前端 未结 12 994
太阳男子
太阳男子 2020-11-22 00:09

I\'m new to Android and I\'m trying to use the UI-Thread, so I\'ve written a simple test activity. But I think I\'ve misunderstood something, because on clicking the button

12条回答
  •  佛祖请我去吃肉
    2020-11-22 00:40

    Below is corrected Snippet of runThread Function.

    private void runThread() {
    
        new Thread() {
            public void run() {
                while (i++ < 1000) {
                    try {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                btn.setText("#" + i);
                            }
                        });
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    

提交回复
热议问题