2011-06-07 36 views
1

为什么某些ui更新可以在特殊情况下在Non_UI线程上运行? 例如:某些ui更新可以在特殊情况下在Non_UI线程上运行

Textview textView; 
public void onCreate(Bundle savedInstanceState) { 
    textView = (TextView) findViewById(R.id.my_text); 
    new Thread() { 
     textView.setText("i can change the textview's text"); 
    }.start(); 
} 

我很困惑,当机器人到检查UI动作是在UI线程上运行?一段时间之后?

回答

0

remembre是唯一线程谁可以改变UI是UIThread, 所以如果你想你的UI从另一个线程改变,你前人的精力使用方法runOnUIThread()这样的:

YourAcitivity.this.runOnUIThread(new Runnable(){ 
@Override 
public void run(){ 
textView.setText("i can change the textview's text"); 
} 
}); 

第二点::在你的线程,有没有方法run()

+0

对不起,我错过了运行方法。但新的线程(){ public void run(){ textView.setText(“我可以改变textview的文本”); } } .start();但是这也是罚款。 – asfman 2011-06-09 11:59:12

0

你实际上并没有设置在其他线程中的文本。要在新线程中运行的代码采用run方法,您应该执行该方法。这是正确的语法:

new Thread() { 
    public void run(){ 
     textView.setText("i can change the textview's text"); 
    } 
}.start(); 
相关问题