|
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(TextView)findViewById(R.id.tv);
text.setText("RRRR");
text.setTextColor(Color.GREEN);
new Thread(new Runnable() {
@Override
public void run(){
try{
Thread.sleep(3000);
Message msg = new Message();
//handler发送msg
handler.sendMessage(msg);
d();
}
catch(Exception e){}
}
}).start();
}
//handler接收msg后处理操作
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
d();
}
};
public void d(){
text=(TextView)findViewById(R.id.tv);
text.setText("GGGG");
text.setTextColor(Color.WHITE);
}
子线程不能操作UI线程,同为线程runnable不为阻塞子线程,以上编写方式较好 |
|