复选框(CheckBox)
package com.xx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class CheckBoxDemoActivity extends Activity {
private static final OnCheckedChangeListener CheckedChangeListener = null;
/** Called when the activity is first created. */
private String myresults="";
private CheckBox my1,my2,my3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
my1=(CheckBox) findViewById(R.id.mychoose1);
my1.setChecked(true);
my2=(CheckBox) findViewById(R.id.mychoose2);
my3=(CheckBox) findViewById(R.id.mychoose3);
my1.setOnCheckedChangeListener(CheckedChangeListener1);
my2.setOnCheckedChangeListener(CheckedChangeListener1);
my3.setOnCheckedChangeListener(CheckedChangeListener1);
}
private OnCheckedChangeListener CheckedChangeListener1=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(my1.isChecked()){
myresults=myresults+getString(R.string.item1).toString()+"\t";
}
if(my2.isChecked()){
myresults=myresults+getString(R.string.item2).toString()+"\t";
}
if(my3.isChecked()){
myresults=myresults+getString(R.string.item3).toString()+"\t";
}
TextView textView=(TextView) findViewById(R.id.results);
textView.setText(myresults);
myresults="";
}
};
}
main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/results"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<CheckBox
android:id="@+id/mychoose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1" />
<CheckBox
android:id="@+id/mychoose2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2" />
<CheckBox
android:id="@+id/mychoose3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3" />
</LinearLayout>
实现结果:



浙公网安备 33010602011771号