Android include的使用,监听点击事件

这是include的条目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#f0f0f0"
android:dividerHeight="0.5dp"
android:background="#ffffff">

</ListView>
<Button android:id="@+id/tuikuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退款"
android:layout_gravity="right"/>
</LinearLayout>
 <include android:id="@+id/iclude1" layout="@layout/listviewitem"/>

 <include android:id="@+id/iclude2" layout="@layout/listviewitem"/>

这是获得include中的控件

View include = (View) findViewById(R.id.iclude1);
View include2 = (View) findViewById(R.id.iclude2);

ListView list = (ListView) include.findViewById(R.id.listview1);
ListView list2 = (ListView) include2.findViewById(R.id.listview1);
tui = (Button) include.findViewById(R.id.tuikuan);
tui2 = (Button) include2.findViewById(R.id.tuikuan);

@Override
public void onClick(View v) {
//获得父控件的对象,然后获得父控件的id
ViewGroup parent = (ViewGroup) v.getParent();
switch (parent.getId()) {
case R.id.iclude1:
if (v.getId() == R.id.tuikuan) {
tui.setVisibility(View.GONE);
Toast.makeText(this, "退款成功", Toast.LENGTH_SHORT).show();
}
break;
case R.id.iclude2:
if (v.getId() == R.id.tuikuan) {
tui2.setVisibility(View.GONE);
Toast.makeText(this, "退款成功", Toast.LENGTH_SHORT).show(); }
break;
}
}

 

posted @ 2021-04-26 21:56  卧龙传奇  阅读(532)  评论(0编辑  收藏  举报