关闭

handler更新UI四种方式的简单实现

38人阅读 评论(0) 收藏 举报
package com.test.day0829;


import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {
private TextView tv;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText("Bei Jing!");
};
};
private void handler1(){
handler.post(new Runnable() {

@Override
public void run() {
tv.setText("Nan Jing!");
}
});
}
private void handler2(){
handler.sendEmptyMessage(0);
}
private void updateUI(){
runOnUiThread(new Runnable() {

@Override
public void run() {
tv.setText("Shang Hai!");
}
});
}
private void viewUI(){
tv.post(new Runnable() {

@Override
public void run() {
tv.setText("Shen Zhen!");
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
new Thread(){
public void run() {
try {
Thread.sleep(10000);
updateUI();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

};
}.start();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:260次
    • 积分:62
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档