Android笔记——Handler更新UI示例

来源:互联网 发布:2016全球社交网络排行 编辑:程序博客网 时间:2024/06/07 09:01
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() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message=new Message();Bundle bundle =new Bundle();bundle.putString("name", "Hello"+i);i++;message.setData(bundle);handler.sendMessage(message);}}, 1000,1000);                    }}

0 0
原创粉丝点击