public class TestActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.handler_msg);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 模拟耗时的操作。
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 更新主线程UI
TestActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
btn.setText("更新完毕!");
}
});
}
}).start();
}
});
}
本文转自wanqi博客园博客,原文链接:http://www.cnblogs.com/wanqieddy/p/4153203.html如需转载请自行联系原作者