@Yano
2016-01-03T18:11:25.000000Z
字数 712
阅读 2473
Android
功能:每秒使 TextView 显示内容 +1。
public class MainActivity extends Activity {
private TextView textView;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textView.setText((String) msg.obj);
}
};
new Thread(new thread()).start();
}
// 每秒更新textview
public class thread implements Runnable {
public void run() {
int c = 0;
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 将要更新的内容发给handler
Message msg = new Message();
msg.obj = "" + (c++);
handler.sendMessage(msg);
}
}
}
}