checkbox实现单选,全选,反选,取消选

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="反 选"
         android:layout_weight="1"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全不选"
         android:layout_weight="1"
        />
</LinearLayout>
<ListView
    android:id="@+id/listveiw"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    ></ListView>

</LinearLayout>

  

listveiw.xml

如果ListView的Item中包含CheckBox,由于CheckBox的焦点优先级高于ListView,所以当选中ListView时,ListView将不会有选中的状态,解决的方法就是在checkbox组件中加一属性:android:focusable="false"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   <TextView
       android:id="@+id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ddd"

       />

   <CheckBox
       android:id="@+id/checkbox"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"

        android:focusable="false"  

       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true" />

</RelativeLayout>

  MainActivity.java

package com.bawei.day04_listview;

import java.util.ArrayList;
import java.util.List;

import com.bawei.vo.Good;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    private Button button1;
    private Button button2;
    private Button button3;
    private ListView listView;
    private List<Good> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        listView = (ListView) findViewById(R.id.listveiw);
        list = new ArrayList<Good>();
        for (int i = 0; i < 60; i++) {
            list.add(new Good("PP"+i, false));
        }

        BaseAdapter adapter=new Adper(list,MainActivity.this,button1,button2,button3);
        listView.setAdapter(adapter);

    }

}

  list的vo包Good.java

package com.bawei.vo;

public class Good {
private String name;
private boolean bo;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public boolean getBo() {
    return bo;
}
public void setBo(boolean bo) {
    this.bo = bo;
}
@Override
public String toString() {
    return "Good [name=" + name + ", bo=" + bo + "]";
}
public Good(String name, boolean bo) {
    super();
    this.name = name;
    this.bo = bo;
}
public Good() {
    super();
}

}

  Adper.java

package com.bawei.day04_listview;

import java.util.List;

import com.bawei.vo.Good;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class Adper extends BaseAdapter {
    List<Good> list;
    Context context;
    Button button1;
    Button button2;
    Button button3;
    public Adper(List<Good> list, Context context, Button button1, Button button2, Button button3) {
        // TODO Auto-generated constructor stub
    this.list=list;
    this.context=context;
    this.button1=button1;
    this.button2=button2;
    this.button3=button3;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.listveiw, null);
         viewHolder=new ViewHolder();
         viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
        viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);
        convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(list.get(position).getName());

        //checkbox监听
        viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                 if(isChecked){

                     list.get(position).setBo(isChecked);

                }else{

                    list.get(position).setBo(isChecked);

                }

            }
        });
        //全选
        button1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {
                    //改变boolean
                    list.get(i).setBo(true);
                    //刷新
                    notifyDataSetChanged();
                }

            }
        });

        //反选
        button2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getBo()){
                        list.get(i).setBo(false);
                    }else{
                        list.get(i).setBo(true);
                    }
                    notifyDataSetChanged();
                }
            }
        });
        //全不选
        button3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {

                    list.get(i).setBo(false);

                    notifyDataSetChanged();
                }
            }
        });
        //显示checkBox
        viewHolder.checkBox.setChecked(list.get(position).getBo());
        return convertView;
    }
class ViewHolder{
    TextView textView;
    CheckBox checkBox;
}
}

  

时间: 2024-09-30 14:25:54

checkbox实现单选,全选,反选,取消选的相关文章

全选反选取消-js代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

jquery 全选 反选 取消 批量删除

首选引入jquery 全选 //全选 function myall(){ $("input[name='d[]']").each(function(index,e){ $(this).prop('checked',true); }); } 取消 //当true改为false时为取消 // function no(){ // $("input[name='d[]']").each(function(index,e){ // $(this).prop('checked'

checkbox全选 反选 不选 并获取id的值

$("#lblContents :checkbox").each(function () {                 if (this.checked) {                     this.checked = false;                     ids ="";                 }                 else {                     this.checked = true;

全选-反选-取消

1. .checked=false/true: 作用是给方形框勾上或者取消. checkbox.checked=true; 表示勾选上了. 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display:none } .c1

复选框 全选 反选 不选

<script language="javascript"> var put=document.all.tags("input"); //全选function selall(){for (i=0;i<put.length;i++){   if (put[i].type=="checkbox")    put[i].checked=true;}} //不选function Nselall(){for (i=0;i<put.l

input -多选 反选 不选

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> div{ width: 1000px; height: 200px; background-color: lavender; box-shadow: 5px 5px 2px lightblue; }

checkbox的单选全选,反选,计算价格,删除

activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" a

全选反选取消

http://www.mamicode.com/info-detail-1466046.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .edit-mode{ padding:10px; } .editing{ padding:10px;

JavaScript之全选/反选/取消,for循环闭包

需求:table中要求点击红色单元格和点击checkbox的效果一样,最后一行点击就是全选.反选.取消选择的效果. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> table{ width: 400px; height: 15px; backgrou

jQuery 之 多选反选取消实例

代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.js"></script> </head> <body> <table border="1&qu