在layout的xml中定义的控件,可以加入一个id属性(android:id),以便在代码中访问该控件:
- <TextView
- android:id="@+id/my_text_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
在java代码中,可以这样来操作这个控件:
- TextView tv = (TextView)findViewById(R.id.my_text_view);
- tv.setText("Another string");
我们同样可以为控件添加事件处理:
- tv.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- Toast.makeText(getApplicationContext(), "Clicked!", Toast.LENGTH_SHORT).show();
- }
- });
阅读(1133) | 评论(0) | 转发(0) |