android 每隔2秒执行_android每隔2秒钟显示一个数据
public class TestActivity extends Activity{private TextView mTextView;private Handler mHandler;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCre
public class TestActivity extends Activity{
private TextView mTextView;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
mTextView = (TextView) findViewById(R.id.text);
mHandler.post(new Runnable() {
@Override
public void run()
{
// TODO Auto-generated method stub
mTextView.setText(String.valueOf((Math.random() * 100)));
mHandler.postDelayed(this, 2000);
}
});
}
}
main.xml中就是一个textview。
代码就是通过Handler不断把写textview的线程放到消息队列中,系统从消息队列获取消息后,就在UI线程绘制textview。
更多推荐
所有评论(0)