Android4.2中简单实现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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <CheckBox
     android:id="@+id/eatId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="吃饭"   
    />
   
    <CheckBox
     android:id="@+id/sleepId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="睡觉"   
    />
   
    <CheckBox
     android:id="@+id/playId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="打豆豆"   
    />
   
    <CheckBox
     android:id="@+id/allId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="全选"   
    />
   
    <CheckBox
     android:id="@+id/oppositeId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="反选"   
    />
   
    <CheckBox
     android:id="@+id/allnoId"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="全不选"   
    />
</LinearLayout>

 

MainActivity类:

package com.mcm.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;

public class MainActivity extends Activity {
 
 private CheckBox eatBox;
 private CheckBox sleepBox;
 private CheckBox playBox;
 private CheckBox allBox;
 private CheckBox oppositeBox;
 private CheckBox allnoBox;
 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        eatBox= (CheckBox) findViewById(R.id.eatId);
        sleepBox= (CheckBox) findViewById(R.id.sleepId);
        playBox= (CheckBox) findViewById(R.id.playId);
        allBox = (CheckBox) findViewById(R.id.allId);
        oppositeBox = (CheckBox) findViewById(R.id.oppositeId);
        allnoBox = (CheckBox) findViewById(R.id.allnoId);
        /*
        CheckBoxListener listener = new CheckBoxListener();
        eatBox.setOnClickListener(listener);
        sleepBox.setOnClickListener(listener);
        playBox.setOnClickListener(listener);
        */
       
        CheckBoxListener listener = new CheckBoxListener();
        eatBox.setOnCheckedChangeListener(listener);
        sleepBox.setOnCheckedChangeListener(listener);
        playBox.setOnCheckedChangeListener(listener);
        allBox.setOnCheckedChangeListener(listener);
        oppositeBox.setOnCheckedChangeListener(listener);
        allnoBox.setOnCheckedChangeListener(listener);
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
    class CheckBoxListener implements OnCheckedChangeListener{
  @Override
  public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
   if(buttonView.getId() == R.id.eatId){
    System.out.println("eatBox");
   }else if(buttonView.getId() == R.id.sleepId){
    System.out.println("sleepBox");
   }else if(buttonView.getId() == R.id.playId){
    System.out.println("playBox");
   }else if(buttonView.getId() == R.id.allId){//全选
    System.out.println("allBox");
    if(isChecked == true){
     eatBox.setChecked(true);
     sleepBox.setChecked(true);
     playBox.setChecked(true);
    }
    oppositeBox.setChecked(false);
    allnoBox.setChecked(false);
    
   }else if(buttonView.getId() == R.id.oppositeId){//反选
    System.out.println("oppositeBox");
    if(eatBox.isChecked() == true){
     eatBox.setChecked(false);
    }else{
     eatBox.setChecked(true);
    }
    if(sleepBox.isChecked() == true){
     sleepBox.setChecked(false);
    }else{
     sleepBox.setChecked(true);
    }
    if(playBox.isChecked() == true){
     playBox.setChecked(false);
    }else{
     playBox.setChecked(true);
    }    
    allBox.setChecked(false);    
    allnoBox.setChecked(false);

   }else if(buttonView.getId() == R.id.allnoId){//全不选
    System.out.println("allnoBox");
    if(isChecked == true){
     eatBox.setChecked(false);
     sleepBox.setChecked(false);
     playBox.setChecked(false);
     allBox.setChecked(false);
        oppositeBox.setChecked(false);
    }
   }
   /*
   if(isChecked == true){
    System.out.println("checked");
   }else{
    System.out.println("unchecked");
   }
   */
  }
 }

    //CheckBox的使用方法
    /*
    class CheckBoxListener implements OnClickListener{
  @Override
  public void onClick(View v) {
   if(v.getId() == R.id.eatId){
    System.out.println("eatBox");
   }else if(v.getId() == R.id.sleepId){
    System.out.println("sleepBox");
   }else if(v.getId() == R.id.playId){
    System.out.println("playBox");
   }
   CheckBox box = (CheckBox) v;
   if(box.isChecked()){
    System.out.println("checked");
   }else{
    System.out.println("unchecked");
   }
  }
    }
    */
   
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值