CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.gby.s01_e09_checkbox.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    <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/dotaId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dota"        />

    <!--<Button-->
        <!--android:id="@+id/bt_selectall"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="全选" />-->
    <!--<Button-->
        <!--android:id="@+id/bt_reverseSelection"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="反选" />-->
    <!--<Button-->
        <!--android:id="@+id/bt_deselectall"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="取消选择" />-->

</LinearLayout>

MainActivity.java

package com.example.gby.s01_e09_checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox dotaBox;

    @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);
        dotaBox = (CheckBox)findViewById(R.id.dotaId);

        CheckBoxListener listener = new CheckBoxListener();//声明实例化 监听器对象
        eatBox.setOnCheckedChangeListener(listener);//设置setOn监听器CheckedChangeListener,参数是监听器对象
        sleepBox.setOnCheckedChangeListener(listener);//不论哪个控件被点击,都会调用onCheckedChanged()方法
        dotaBox.setOnCheckedChangeListener(listener);

//        OnBoxClickListener listener = new OnBoxClickListener();//声明listener实例化OnClickListner的被实现对象OnBoxClickListener
//        eatBox.setOnClickListener(listener);//1个监听器可以绑定在多个控件上
//        sleepBox.setOnClickListener(listener);
//        dotaBox.setOnClickListener(listener);
    }
//OnCheckedChangeListener,CompoundButton的接口,可以理解为转为CheckBox设计的
    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.getId() == R.id.eatId){//判断被选中的是哪个CheckBox多选按钮的ID
                System.out.println("eatBox");
            }
            else if (buttonView.getId() == R.id.sleepId){
                System.out.println("sleepBox");
            }
            else if (buttonView.getId() == R.id.dotaId){
                System.out.println("dotaBox");
            }

            if (isChecked){//判断 选中状态 的 布尔值boolean
                System.out.println("checked");
            }
            else {
                System.out.println("unchecked");
            }
        }
    }
    //OnClickListener的使用方法
//    class OnBoxClickListener implements View.OnClickListener{
    @Override
//        public void onClick(View view) {
//            CheckBox box = (CheckBox)view;//向下转型
//System.out.println("id-->"+view.getId());//打印view.id
//            if (box.getId() == R.id.eatId){//判断是哪个checkBox的ID,并且打印相应的语句
//                System.out.println("eatBox");
//
//            }
//            else if (box.getId() == R.id.sleepId){
//                System.out.println("sleepBox");
//            }
//            else if (box.getId() == R.id.dotaId){
//                System.out.println("dotaBox");
//            }
//            if ( box.isChecked();){//返回是否被选中状态
//                System.out.println("checked");
//            }
//            else{
//                System.out.println("unchecked");
//            }
//            System.out.println("Checkbox is clicked");
//        }
//      }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值