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;
}
}
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;
}
}