*CheckBox的方法:*
isChecked方法:是否选中
setChecked方法:设置方块状态
toggle方法:切换状态
setOnCheckedChangeListener方法:设置监听器
一个简单的checkbox例子:
MainActivity .java
public class MainActivity extends Activity {
private CheckBox checkbox1;
private CheckBox checkbox2;
private CheckBox checkbox3;
private CheckBox checkbox4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkbox1=(CheckBox)findViewById(R.id.checkbox1);
checkbox2=(CheckBox)findViewById(R.id.checkbox2);
checkbox3=(CheckBox)findViewById(R.id.checkbox3);
checkbox4=(CheckBox)findViewById(R.id.checkbox4);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
setTitle(checkbox1.getText()+"被选中");
}else {
setTitle(checkbox1.getText()+"取消选中");
}
}
});
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
setTitle(checkbox2.getText()+"被选中");
}else {
setTitle(checkbox2.getText()+"取消选中");
}
}
});
checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
setTitle(checkbox3.getText()+"被选中");
}else {
setTitle(checkbox3.getText()+"取消选中");
}
}
});
checkbox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
setTitle(checkbox4.getText()+"被选中");
}else {
setTitle(checkbox4.getText()+"取消选中");
}
}
});
}
@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;
}
public void GetResult(View v){
String result="";
if(checkbox1.isChecked()){
result+=checkbox1.getText()+"、";
}
if(checkbox2.isChecked()){
result+=checkbox2.getText()+"、";
}
if(checkbox3.isChecked()){
result+=checkbox3.getText()+"、";
}
if(checkbox4.isChecked()){
result+=checkbox4.getText()+"、";
}
if(!"".equals(result)){
result=result.substring(0,result.length()-1);
}else{
result="No one";
}
Toast.makeText(getApplicationContext(), result, 0).show();
}
}
资源文件 main.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" >
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textview"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/box1"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/box2"
/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/box3"
/>
<CheckBox
android:id="@+id/checkbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/box4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="GetResult"
/>
</LinearLayout>