Android笔记——Handler更新UI示例

时间:2023-03-08 21:59:09
public class MainActivity extends ActionBarActivity {
private TextView textView;
private int i=0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.textView1);
final Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
Bundle bundle=msg.getData();
String string=bundle.getString("name");
textView.setText(string);
};
};
Timer timer=new Timer();
timer.schedule(new TimerTask() { @Override
public void run() {
// TODO Auto-generated method stub
Message message=new Message();
Bundle bundle =new Bundle();
bundle.putString("name", "Hello"+i);
i++;
message.setData(bundle);
handler.sendMessage(message);
}
}, 1000,1000); }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。