前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener

CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener

作者头像
全栈程序员站长
发布2022-06-26 11:50:06
4680
发布2022-06-26 11:50:06
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

activity_main.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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");
//        }
//      }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133951.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档