<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/allChoice"
android:id="@+id/allChoiceCheckBox"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/otherChoice"
android:layout_toRightOf="@id/allChoiceCheckBox"
android:layout_alignTop="@id/allChoiceCheckBox"
android:id="@+id/otherChoiceCheckBox"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<CheckBox
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/java"
android:id="@+id/javaCheckBox"
/>
<CheckBox
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/php"
android:layout_toRightOf="@id/javaCheckBox"
android:layout_alignTop="@id/javaCheckBox"
android:id="@+id/phpCheckBox"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<CheckBox
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/android"
android:id="@+id/androidCheckBox"
/>
<CheckBox
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/oracle"
android:layout_toRightOf="@id/androidCheckBox"
android:layout_alignTop="@id/androidCheckBox"
android:id="@+id/oracleCheckBox"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/addCheckBox"
>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20px"
>
<Button
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/getValue"
android:id="@+id/getValueButton"
/>
<Button
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/addDiy"
android:layout_toRightOf="@id/getValueButton"
android:layout_alignTop="@id/getValueButton"
android:id="@+id/addDiyButton"
/>
</RelativeLayout>
</LinearLayout>
上面是布局文件
下面是MainActivity.java
package com.android.checkbox.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private CheckBox allChoiceCheckBox;
private CheckBox otherChoiceCheckBox;
private CheckBox javaCheckBox;
private CheckBox phpCheckBox;
private CheckBox androidCheckBox;
private CheckBox oracleCheckBox;
private Button getValueButton;
private Button addDiyButton;
private ArrayList<CheckBox> checkBoxList=new ArrayList<CheckBox>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
allChoiceCheckBox=(CheckBox) this.findViewById(R.id.allChoiceCheckBox);
otherChoiceCheckBox=(CheckBox) this.findViewById(R.id.otherChoiceCheckBox);
javaCheckBox=(CheckBox) this.findViewById(R.id.javaCheckBox);
phpCheckBox=(CheckBox) this.findViewById(R.id.phpCheckBox);
androidCheckBox=(CheckBox) this.findViewById(R.id.androidCheckBox);
oracleCheckBox=(CheckBox) this.findViewById(R.id.oracleCheckBox);
checkBoxList.add(javaCheckBox);
checkBoxList.add(phpCheckBox);
checkBoxList.add(androidCheckBox);
checkBoxList.add(oracleCheckBox);
getValueButton=(Button) this.findViewById(R.id.getValueButton);
addDiyButton=(Button) this.findViewById(R.id.addDiyButton);
// 全选
allChoiceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for( CheckBox checkBox : checkBoxList)
{
checkBox.setChecked(isChecked);
}
}
});
//反选
otherChoiceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for( CheckBox checkBox : checkBoxList)
{
checkBox.setChecked(!checkBox.isChecked());
}
//allChoiceCheckBox.setChecked(false);
}
});
getValueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
StringBuffer sb=new StringBuffer();
for( CheckBox checkBox : checkBoxList)
{
if(checkBox.isChecked())
{
sb.append(checkBox.getText()).append(",");
}
}
sb.deleteCharAt(sb.length()-1);
Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
}
});
addDiyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout addCheckBox=(RelativeLayout) MainActivity.this.findViewById(R.id.addCheckBox);
CheckBox cb=new CheckBox(MainActivity.this);
// LayoutParams params=new LayoutParams();
// cb.setLayoutParams(params)
//addCheckBox.addView(child, params)
}
});
}
}