1、values/string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Activity07!</string> <string name="app_name">Activity07</string> <string name="male">男</string> <string name="female">女</string> <string name="swim">游泳</string> <string name="run">跑步</string> <string name="read">读书</string> </resources>
2、layout/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" > <RadioGroup android:id="@+id/grenderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /> </LinearLayout>
3、Activity
package mars.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Activity07 extends Activity {
/** Called when the activity is first created. */
private RadioGroup grenderGroup = null;
private RadioButton male = null, female = null;
private CheckBox swim = null, run = null, read = null;
private int maleId = 0, femaleId = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grenderGroup = (RadioGroup) super.findViewById(R.id.grenderGroup);
male = (RadioButton) super.findViewById(R.id.male);
female = (RadioButton) super.findViewById(R.id.female);
swim = (CheckBox) super.findViewById(R.id.swim);
run = (CheckBox) super.findViewById(R.id.run);
read = (CheckBox) super.findViewById(R.id.read);
grenderGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == male.getId()) {
System.out.println("Male");
} else if (checkedId == female.getId()) {
System.out.println("Female");
}
}
});
swim.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("Swimming");
}
}
});
run.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("Running");
//吐司,就把它理解为弹出来的意思
Toast.makeText(Activity07.this, "Running", Toast.LENGTH_SHORT).show();
}
}
});
read.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("Readding");
}
}
});
}
}
d