CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener




    

    
    
    

    
        
        
        
        
    
        
        
        
        
    
        
        
        
        


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");
//        }
//      }
}

你可能感兴趣的:(CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener)