CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener

CheckBox多选按钮实现CompoundButton.OnCheckedChangeListeneractivity_main.xml

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

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");
//        }
//      }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号