请问:
做了一个组合控件,里面包含了4个Button,在某个画面中调用。我要在画面上对组合控件中的某个按钮添加事件,怎么获取这个子控件呢?
做了一个组合控件,里面包含了4个Button,在某个画面中调用。我要在画面上对组合控件中的某个按钮添加事件,怎么获取这个子控件呢?
解决方案 »
- eclipse xml的提示代码变成白色了 完全看不清!!! 在哪改。。
- google 语音搜索API
- Android 中文 API (100) —— ScrollView
- 预测APP获奖名单,赢华为手机(包邮哦!亲)还有无敌的VIP权限免费下载哦!
- 求推荐书籍,想深入了解ANDROID开发编程及内核
- android weight的移动效果
- 2d绘图用 ShapeDrawable.draw(canvas) 和 canvas.drawXXX 有啥区别?
- 沒有放Button的工具栏?
- VS2010使用Mono为什么我新建的项目中layout文件夹下的Main.axml使用设计视图浏览时出现"未能完成操作,尚未实现"
- 求问android:layout_weight用法
- 求解决google mapview的问题
- 我有个android2.2系统的平板电脑,现在开机老停在小绿人那,进不去,怎么办
public class TestActivity extends Activity { private LinearLayout_Button linLayBtn; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); linLayBtn= (LinearLayout_Button) findViewById(R.id.linLayBtn);
//获取LinearLayout_Button中的Button
}
}
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@android:color/white">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
android:textColor="@android:color/black" android:layout_weight="1" />
<com.test.LinearLayout_Button
android:id="@+id/linLayBtn"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</com.test.LinearLayout_Button>
</LinearLayout>
组合控件:public class LinearLayout_Button extends LinearLayout {
public LinearLayout_Button(Context context) {
this(context, null);
} public LinearLayout_Button(Context context, AttributeSet attrs) {
super(context, attrs); LayoutInflater.from(getContext()).inflate(
R.layout.linlay_btn, this);
}
}
组合控件XML:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Button1" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button2" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button3" android:id="@+id/button3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button4" android:id="@+id/button4"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
Button btn=(Button)findViewById(...);
.....
linearyayout应为layout文件下地xml的名字 R.layout.linearyayoutButton btn = (Button ) layout.findViewById(R.id.btn);
就在自定义控件的类里面加个 GetButton 方法就行了。明天结贴!