原CSDN博客已弃用,文章会逐渐迁移过来。

转帖请注明本文出自weimeig的博客(https://blog.csdn.net/weimeig/article/details/79662379),请尊重他人的辛勤劳动成果,谢谢

应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Android中的CheckBox多选框。

布局文件 activity_checkbo.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal">  
        <TextView  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="请选择爱好:"  
            android:layout_weight="1"/>  
    <CheckBox  
        android:id="@+id/dianjing_cb"  
        android:layout_width="0dp"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"  
        android:text="电竞"  
        />  
        <!--   android:checked="true" 设置默认为选定状态 
        -->  
        <CheckBox  
            android:id="@+id/lvyou_cb"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="旅游"  
            android:layout_weight="1"/>  
        <CheckBox  
            android:id="@+id/dushu_cb"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:text="读书"/>  


    </LinearLayout>  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal">  
        <Button  
            android:id="@+id/all_btn"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="全选"/>  
        <Button  
            android:id="@+id/notall_btn"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="全不选"/>  
    </LinearLayout>  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal">  
        <Button  
            android:id="@+id/getResult_btn"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="获取内容"/>  
        <TextView  
            android:id="@+id/showResult_tv"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:hint="此文本框用于显示结果"/>  
    </LinearLayout>  
</LinearLayout>  

Java代码

import android.graphics.Color;  
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;  
import android.widget.Toast;  

import java.util.ArrayList;  
import java.util.List;  

public class MainActivity extends AppCompatActivity  {  
    private CheckBox dianJing,lvYou,duShu;  
    private CheckBoxListener checkBoxListener;  
    private ButtonListener btnButtonListener;  
    private Button all,notall,getResult;  
    private TextView showResult;  
    private List<String> lists;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_checkbo);  
        initView();  
        initData();  
        setListener();  
    }  
    private void initView(){  
        /** 
         * findViewById初始化控件 
         */  
        dianJing = findViewById(R.id.dianjing_cb);  
        lvYou = findViewById(R.id.lvyou_cb);  
        duShu = findViewById(R.id.dushu_cb);  
        all = findViewById(R.id.all_btn);  
        notall = findViewById(R.id.notall_btn);  
        showResult = findViewById(R.id.showResult_tv);  
        getResult = findViewById(R.id.getResult_btn);  

    }  
    private void setListener(){  
        /** 
         *  绑定监听器 
         */  

        checkBoxListener = new CheckBoxListener();  
        btnButtonListener = new ButtonListener();  
        duShu.setOnCheckedChangeListener(checkBoxListener);  
        lvYou.setOnCheckedChangeListener(checkBoxListener);  
        dianJing.setOnCheckedChangeListener(checkBoxListener);  
        all.setOnClickListener(btnButtonListener);  
        notall.setOnClickListener(btnButtonListener);  
        getResult.setOnClickListener(btnButtonListener);  
    }  
    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{  
        /** 
         * 找到ID 
         * @param buttonView 
         * @param isChecked 
         */  
        @Override  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
            CheckBox checkBox = (CheckBox) buttonView;  
            switch (checkBox.getId()){  
                case R.id.dianjing_cb:  
                   if(isChecked){  
                       Toast.makeText(MainActivity.this,"少玩游戏多谢代码!"+isChecked,1000).show();  
                        dianJing.setTextColor(Color.RED);  
                   }else{  
                       Toast.makeText(MainActivity.this,"电竞" +isChecked,1000).show();  
                       dianJing.setTextColor(Color.BLACK);  
                   }  
                    break;  
                case R.id.lvyou_cb:  
                    Toast.makeText(MainActivity.this,"旅游" +isChecked,1000).show();  
                    break;  
                case R.id.dushu_cb:  
                    Toast.makeText(MainActivity.this,"读书" +isChecked,1000).show();  
                    break;  
            }  
        }  
    }  
    private void initData(){  
        /** 
         * 获取生成集合对象 
         */  
        lists = new ArrayList<String>();  
    }  
   class ButtonListener implements View.OnClickListener{  

       @Override  
       public void onClick(View v) {  
           switch (v.getId()){  
               case R.id.all_btn:  
                    dianJing.setChecked(true);  
                   lvYou.setChecked(true);  
                   duShu.setChecked(true);  
                     break;  
               case R.id.notall_btn:  
                   dianJing.setChecked(false);  
                   lvYou.setChecked(false);  
                   duShu.setChecked(false);  
                   break;  
               case R.id.getResult_btn:  
                    if(dianJing.isChecked()){  
                        lists.add(dianJing.getText().toString());  
                    }  
                    if(lvYou.isChecked()){  
                        lists.add(lvYou.getText().toString());  
                    }  
                    if(duShu.isChecked()){  
                        lists.add(duShu.getText().toString());  
                    }  
                    showResult.setText(lists.toString());  
                    lists.clear();  
                   break;  

           }  
       }  
   }  
}