Android笔记——Handler更新UI示例

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

 

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

 

posted @ 2015-06-13 11:02  Pwcong  阅读(230)  评论(0编辑  收藏  举报