您的位置:首页 > 产品设计 > UI/UE

在子线程中,更新ui的几种方法

2016-03-18 10:33 465 查看
public class MainActivity extends Activity {
TextView tv ;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
method();
}

int count = 1;
private boolean isStart = true;
private void method(){
new Thread(){

public void run() {
while(isStart){
/**
* 方法一
*/
/* MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
tv.setText(""+count);
//count++;
}
});*/

/**
* 方法二
*/

/*tv.post(new Runnable() {

@Override
public void run() {
tv.setText(""+count);
//SystemClock.sleep(1000);
}
});*/

//SystemClock.sleep(1000);
//count++;

}
};
}.start();
/**
* 方法三
*/
handler.postDelayed(new Runnable() {

@Override
public void run() {
tv.setText(count+"");
handler.postDelayed(this, 1000);
count++;
}
}, 1000);
}
@Override
protected void onDestroy() {
super.onDestroy();
isStart = false;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: