要实现ListView中CheckBox的选中并记录,我的做法是自己写一个Adapter,继承于BaseAdapter,ListView中的CheckBox需要将focusable,clickable的属性都设置为false,防止CheckBox将点击事件拦截,在ListView的onItemClick方法中接收点击事件并对数据进行处理。废话不多说,上代码。
先给大家看一下listview中数据的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<CheckBox
android:id="@+id/cb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="false"
android:layout_alignParentRight="true" />
</RelativeLayout>
然后再看main.xml

本文介绍了如何在Android开发中实现在ListView中包含CheckBox,并处理点击事件来实现选中记录。关键在于自定义Adapter继承BaseAdapter,设置CheckBox的focusable和clickable属性为false,以及在ListView的onItemClick方法中处理数据。
最低0.47元/天 解锁文章
533

被折叠的 条评论
为什么被折叠?



