实现ListView中的CheckBox的全选、反选、全不选

android如何实现ListView中的CheckBox的全选、反选、全不选 (2011-10-28 14:11:55)转载▼ 标签: it 分类: 移动手机应用开发 刚刚接触android开发一个月左右,因为公司项目的需要,我不得不马上将所学用于android平台下智能系统的开发,在开发中经常遇到问题,当然我也在这些问题中一步步成长。今天上午我遇到的问题是如何实现ListView中的CheckBox的全选、反选、全不选的功能。在网上查找了很多资料,但是贴上来都不适用,最后东拼西凑,然后结合自己项目的实际情况自己写出了代码实现了需要的功能。好了,在此我将代码贴在下面,供大家学习交流之用。

1、全选

 for (int index = 0; index < controlActionView.getChildCount(); index++) { 
   LinearLayout layout = (LinearLayout) controlActionView.getChildAt(index);
   CheckBox checkBox = (CheckBox) layout.findViewById(R.id.isselected);
   checkBox.setChecked(true);
}

 

2、反选

for (int index = 0; index < controlActionView.getChildCount(); index++) { 
LinearLayout layout = (LinearLayout) controlActionView.getChildAt(index);
CheckBox checkBox = (CheckBox) layout.findViewById(R.id.isselected);

if (checkBox.isChecked()) {
  checkBox.setChecked(false);
} else {
  checkBox.setChecked(true);
}
}

 

 3、全不选

for (int index = 0; index < controlActionView.getChildCount(); index++) {
  LinearLayout layout = (LinearLayout) controlActionView.getChildAt(index);
   CheckBox checkBox = (CheckBox) layout.findViewById(R.id.isselected);
  checkBox.setChecked(false);
}

 

 看了上面的代码是不是很简单呢,其实知识都这样,编程更是如此。在遇到问题时,感觉这个问题是多么的深不可测。然后通过各种方式去查找资料解决问题。当我们找到解决方案时,感觉都很简单。该ListView中的布局文件如下: 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="@+id/controlaction_id"
android:layout_height="0dip"
android:layout_width="0dip"
/>
<TextView
android:id="@+id/controlaction_code"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:textSize="15pt"
/>
<TextView
android:id="@+id/controlaction_name"
android:layout_height="wrap_content"
android:layout_width="180dip"
android:textSize="8pt"
/>
<CheckBox
android:id="@+id/isselected"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值