Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1736495
  • 博文数量: 438
  • 博客积分: 9799
  • 博客等级: 中将
  • 技术积分: 6092
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 17:25
文章分类

全部博文(438)

文章存档

2019年(1)

2013年(8)

2012年(429)

分类: 嵌入式

2012-03-25 21:01:27

在layout的xml中定义的控件,可以加入一个id属性(android:id),以便在代码中访问该控件:


  1. <TextView
  2.     android:id="@+id/my_text_view"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:text="@string/hello"
  6.     />

在java代码中,可以这样来操作这个控件:
  1. TextView tv = (TextView)findViewById(R.id.my_text_view);
  2. tv.setText("Another string");

我们同样可以为控件添加事件处理:
  1. tv.setOnClickListener(new OnClickListener(){
  2.     @Override
  3.     public void onClick(View arg0) {
  4.         Toast.makeText(getApplicationContext(), "Clicked!", Toast.LENGTH_SHORT).show();
  5.     }
  6. });

阅读(1082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~