现在的位置: 首页 > 综合 > 正文

Android中ListView结合CheckBox判断选中项

2014年02月04日 ⁄ 综合 ⁄ 共 1177字 ⁄ 字号 评论关闭

本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。

[1] 程序结构图如下:

其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。

[2] listitem.xml布局文件源码如下:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.   <LinearLayout  
  8.      android:layout_width="fill_parent"  
  9.      android:layout_height="wrap_content"  
  10.      android:orientation="horizontal"  
  11.      android:descendantFocusability="blocksDescendants">  
  12.       <CheckBox  
  13.          android:id="@+id/list.select"  
  14.          android:layout_width="wrap_content"  
  15.          android:layout_height="wrap_content"/>  
  16.       <TextView  
  17.          android:id="@+id/list.name"  
  18.          android:layout_width="fill_parent"  
  19.          android:layout_height="wrap_content"  
  20.          android:layout_weight="1"  
  21.          android:text="Name"  
  22.          android:layout_gravity="center"  
  23.          android:textSize="20dp"  
  24.          android:layout_marginLeft="10dp"/>  
  25.       <

抱歉!评论已关闭.