本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。
[1] 程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。
[2] listitem.xml布局文件源码如下:
- <?xmlversion="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">
-
<LinearLayout
-
Android:layout_width="fill_parent"
-
Android:layout_height="wrap_content"
-
Android:orientation="horizontal"
-
Android:descendantFocusability="blocksDescendants">
-
<CheckBox
-
Android:id="@+id/list.select"
-
Android:layout_width="wrap_content"
-
Android:layout_height="wrap_content"/>
-
<TextView
-
Android:id="@+id/list.name"
-
Android:layout_width="fill_parent"
-
Android:layout_height="wrap_content"
-
Android:layout_weight="1"
-
Android:text="Name"
-
Android:layout_gravity="center"
-
Android:textSize="20dp"
-
Android:layout_marginLeft="10dp"/>
-
<TextView
-
Android:id="@+id/list.address"
-
Android:layout_width="fill_parent"
-
Android:layout_height="wrap_content"
-
Android:layout_weight="1"
-
Android:text="Address"
-
Android:layout_gravity="center"
-
Android:textSize="20dp"/>
-
</LinearLayout>
-
</LinearLayout>
[3] main.xml布局文件源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
-
Android:orientation="vertical"
-
Android:layout_width="fill_parent"
-
Android:layout_height="fill_parent">
-
<Button
-
Android:id="@+id/show"
-
Android:layout_width="fill_parent"
-
Android:layout_height="wrap_content"
-
Android:text="Show"/>
-
<ListView
-
Android:id="@+id/lvperson"
-
Android:layout_width="fill_parent"
-
Android:layout_height="fill_parent"/>
-
</LinearLayout>
[4] Person.java实体类源码如下:
- packagecom.andyidea.bean;
- publicclassPerson{
- privateStringname;
- privateStringaddress;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
-
this.name=name;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- publicvoidsetAddress(Stringaddress){
-
this.address=address;
- }
- }
[5] MainActivity.java类源码如下:
[6] 程序运行后的结果如下:
分享到:
相关推荐
Android中ListView结合CheckBox结合,随意实现获取选中值,可以实现实战中类似的大部分需求
实现ListView上CheckBox的选择动态改变选中项的数目以及提取选中项信息,可以运用到购物车结算模块中
这个项目是Android studio的,eclipse的朋友可以复制一下资源就能用了。...4、计算选中的项数已经相应UI的隐显增强用户体验;5、自定义AlertDialog布局。全部自己研究编写,测试运行Ok,为真实项目的所用的简化Demo。
android listview和checkbox联合使用,选中和取消
解决ListView嵌套CheckBox滑动时CheckBox选中状态错乱问题,以及listview中事件点击冲突问题;一个简单的小Demo;
android ListView中的checkBox全选和反选Demo
android listview复选框,包括全选,反选,取消选择,确认删除,androidStudio环境中的demo
主要介绍了Android编程中ListView结合CheckBox实现批量选择,包括实现全选、反选、全不选等操作,感兴趣的小伙伴们可以参考一下
android的listview中包含checkbox,并且有全选反选和清楚选择的操作,效果图见http://www.eoeandroid.com/forum.php?mod=viewthread&tid=300663&page=1&extra=#pid3057257
完美解决了ListView和CheckBox的焦点冲突及CheckBox的复用问题,讲解了CheckBox的全选、反选、删除功能及在下拉刷新和下拉加载中如何保存CheckBox的先前的选中状态
每个listview的每条item中都有多个checkbox,在项目中需要用到,所以写了个小demo,可以判断每个item中哪些checkbox被选中了。
listview实现多线,用一个list记录选中状态
2.选择的checkbox后滑动listview内容时会checkbox选择的值会刷新成原来状态值(即没选中);3.选择listview所有checkbox值之后,只能获取当前可见的checkbox的值,向后滑动选择的值无法获取。 解决以上问题可以采取...
每次ListView只有一个条目的CheckBox可以被选中,而且没有CheckBox复用图片错位的问题。
这个文档是解决listview中添加checkbox控件,选中checkbox后listview上下滑动,checkbox对应的item不发生移位,代码比较简单,适合新手
下载积分被CSDN设置了50积分,非本人所为。没有积分的可以参看这篇文章。文章中代码基本都贴出来了。https://blog.csdn.net/qq_20521573/article/details/52073472
项目中需要有个Dialog全选对话框,点击全选全部选中,取消全选全部取消。下午查了些资料,重写了一下Dialog对话框。把代码放出来。
解决ListView中每个条目中的CheckBox选中后,其他的的条目中的CheckBox也会被选中的问题