1#
安卓Developer QQ群273843464欢迎您的加入~~
原文参考:http://blog.cfxixi.com/post/2012/10/17/%E5%AE%89%E5%8D%93checkbox%E5%BA%94%E7%94%A8%E7%A4%BA%E4%BE%8B.aspx

一个多选框checkbox组(例子可去群共享下载)

TextView    m_TextView1;

    Button        m_Button1;

    CheckBox    m_CheckBox1;
    CheckBox    m_CheckBox2;
    CheckBox    m_CheckBox3;
    CheckBox    m_CheckBox4;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        m_TextView1 = (TextView) findViewById(R.id.TextView1);
        m_Button1 = (Button) findViewById(R.id.button1);

        m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
        m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
        m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
        m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);

        
        m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(m_CheckBox1.isChecked())
                {
                    DisplayToast("checked"+m_CheckBox1.getText());
                }
            }
        });
        ////////////////////
        m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(m_CheckBox2.isChecked())
                {
                    DisplayToast("checked"+m_CheckBox2.getText());
                }
            }
        });
        /////////////////
        m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(m_CheckBox3.isChecked())
                {
                    DisplayToast("checked"+m_CheckBox3.getText());
                }
            }
        });
        ////////////////
        m_CheckBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(m_CheckBox4.isChecked())
                {
                    DisplayToast("ccc"+m_CheckBox4.getText());
                }
            }
        });
    
        m_Button1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v)
            {
                int num = 0;
                if(m_CheckBox1.isChecked())
                {
                    num++;
                }
                if(m_CheckBox2.isChecked())
                {
                    num++;
                }
                if(m_CheckBox3.isChecked())
                {
                    num++;
                }
                if(m_CheckBox4.isChecked())
                {
                    num++;
                }
                DisplayToast("aaa"+num+"bb");
            }
        });
    }
    
    public void DisplayToast(String str)
    {
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 220);
        toast.show();
    }
页面布局:


    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      android:id="@+id/TextView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
  android:id="@+id/CheckBox1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/CheckBox1"
>

  android:id="@+id/CheckBox2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/CheckBox2"
>

android:id="@+id/CheckBox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox3"
>

android:id="@+id/CheckBox4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox4"
>

          android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="提交"
    >