android CheckBox使用和状态获得

android CheckBox使用和状态获得
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <!-- 定义CheckBox控件 ,代表篮球选项-->
    <CheckBox
        android:id="@+id/CbBasketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球" />

    <!-- 定义CheckBox控件 ,代表乒乓球选项-->
    <CheckBox
        android:id="@+id/CbPingpangball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球" />

    <!-- 定义CheckBox控件 ,代表足球选项-->
    <CheckBox
        android:id="@+id/CbFootball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="足球" />

    <!-- 定义TextView控件,来显示选中结果 -->
    <TextView
        android:id="@+id/TvResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str" />

</LinearLayout>
package com.example.yanlei.yl2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
//导入必备的包
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends AppCompatActivity {

    private CheckBox CbBasketball;        //定义篮球的复选框对象
    private CheckBox CbPingpangball;    //定义乒乓球的复选框对象
    private CheckBox CbFootball;        //定义足球的复选框对象
    private TextView TvResult;            //定义结果文本便签对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        //调用父类的onCreate方法

        //通过setContentView方法设置当前页面的布局文件为activity_main
        setContentView(R.layout.activity_main);
        findView();            //获取页面中的控件
        setListener();        //设置控件的监听器
    }

    private void setListener() {
        // TODO Auto-generated method stub
        //设置所有CheckBox的状态改变监听器
        CbBasketball.setOnCheckedChangeListener(myCheckChangelistener);
        CbPingpangball.setOnCheckedChangeListener(myCheckChangelistener);
        CbFootball.setOnCheckedChangeListener(myCheckChangelistener);
    }

    OnCheckedChangeListener myCheckChangelistener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            //设置TextView的内容显示CheckBox的选择结果
            setText();
        }
    };


    private void findView() {
        // TODO Auto-generated method stub
        //通过findViewById得到对应的控件对象
        CbBasketball = (CheckBox)findViewById(R.id.CbBasketball);
        CbPingpangball = (CheckBox)findViewById(R.id.CbPingpangball);
        CbFootball = (CheckBox)findViewById(R.id.CbFootball);
        TvResult = (TextView)findViewById(R.id.TvResult);
    }

    private void setText(){
        String str;
        TvResult.setText("");    //清空TextView的内容
        //如果CbBasketball被选中,则加入TvResult内容显示
        if (CbBasketball.isChecked()) {
            str = TvResult.getText().toString()+CbBasketball.getText().toString()+",";
            TvResult.setText(str);
        }
        //如果CbPingpangball被选中,则加入TvResult内容显示
        if (CbPingpangball.isChecked()) {
            str = TvResult.getText().toString()+CbPingpangball.getText().toString()+",";
            TvResult.setText(str);
        }
        //如果CbFootball被选中,则加入TvResult内容显示
        if (CbFootball.isChecked()) {
            str = TvResult.getText().toString()+CbFootball.getText().toString();
            TvResult.setText(str);
        }
    }


}