实现多选按钮的监听有两种方法:OnClickListener 和 OnCheckedChangeListener

下面是这两种方法的实现:

  1. OnClickListener 的使用方法

     activity_main部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

<LinearLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///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="玩耍"/>


</LinearLayout>

activity_main.xml

     MainActivity.java部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

package com.iddiea.checkbox;

import .Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {
    
    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        eatBox = (CheckBox)findViewById(.eatId);
        sleepBox = (CheckBox)findViewById(.sleepId);
        playBox = (CheckBox)findViewById(.playId);
                                        
        OnBoxClickListener listener = new OnBoxClickListener();
        eatBox.setOnClickListener(listener);
        sleepBox.setOnClickListener(listener);
        playBox.setOnClickListener(listener);
}

class OnBoxClickListener implements OnClickListener{

        @Override
        public void onClick(View view) {
            CheckBox box =(CheckBox)view;
            if (box.getId()==.eatId) {
                System.out.println("eatBox");    
            }
            else if (box.getId()==.sleepId){
                System.out.println("sleepBox");
            }
            else if (box.getId()==.playId){
                System.out.println("playBox");
            }
            
            if(box.isChecked()){
            System.out.println("Checkbox is clicked ");
            }
            else {
                System.out.println("Checkbox is clicked ");    
            }
        }

        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

MainActivity.java

 

  2.  OnCheckedChangeListener 的使用方法

    activity_main部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

<LinearLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///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="玩耍"/>

</LinearLayout>

activity_main

 

     MainActivity.java部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

package com.iddiea.checkbox;

import .Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {
    
    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        eatBox = (CheckBox)findViewById(.eatId);
        sleepBox = (CheckBox)findViewById(.sleepId);
        playBox = (CheckBox)findViewById(.playId);
        
        
        CheckBoxListener listener = new  CheckBoxListener();
        eatBox.setOnCheckedChangeListener(listener);
        sleepBox.setOnCheckedChangeListener(listener);
        playBox.setOnCheckedChangeListener(listener);

}
    
    class CheckBoxListener implements OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if(buttonView.getId()==.eatId){
                System.out.println("eatBox");
            }
            else if (buttonView.getId()==.sleepId){
                System.out.println("sleepBox");
            }
            else if (buttonView.getId()==.playId){
                System.out.println("playBox");        
            }
            
            if(isChecked){
                System.out.println("Checked");
            }
            else {
                System.out.println("UnChecked");
            }
}
        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Main_Activity.java

 

  3. 实现 选中All  自动选中其他 子项

    activity_main部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

<LinearLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///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="All"/>

</LinearLayout>

activity_main.xml

 

     MainActivity部分

android 多选代码 android 多选按钮_Android

android 多选代码 android 多选按钮_ide_02

package com.iddiea.checkbox;

import .Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {
    
    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    private CheckBox allBox;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        eatBox = (CheckBox)findViewById(.eatId);
        sleepBox = (CheckBox)findViewById(.sleepId);
        playBox = (CheckBox)findViewById(.playId);
        allBox = (CheckBox)findViewById(.allId);
        
        
        CheckBoxListener listener = new  CheckBoxListener();
        eatBox.setOnCheckedChangeListener(listener);
        sleepBox.setOnCheckedChangeListener(listener);
        playBox.setOnCheckedChangeListener(listener);
        allBox.setOnCheckedChangeListener(listener);
        
    }
    
    class CheckBoxListener implements OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if (buttonView.getId()==.allId) {
                eatBox.setChecked(isChecked);
                sleepBox.setChecked(isChecked);
                playBox.setChecked(isChecked);
            }

        }
        
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

MainActivity.java

 

运行结果:

android 多选代码 android 多选按钮_android 多选代码_13

android 多选代码 android 多选按钮_ide_14