package com.example.testbutton;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
// 对控件对象进行定义
private RadioGroup genderGroup = null;
private RadioButton male = null;
private RadioButton female = null;
private CheckBox read = null;
private CheckBox swim = null;
private CheckBox run = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过对象ID 取得代表控件的对象
genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
read = (CheckBox) findViewById(R.id.read);
run = (CheckBox) findViewById(R.id.run);
swim = (CheckBox) findViewById(R.id.swim);
// 为RadioGroup设置监听器,注意这里的监听器和Button控件的监听器有所不同
genderGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (female.getId() == checkedId) {
System.out.println("female");
} else if (male.getId() == checkedId) {
System.out.println("male");
}
}
});
// 为多选按钮添加监听器
read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.print("read is checked");
} else {
System.out.print("read is unchecked");
}
}
});
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.print("run is checked");
} else {
System.out.print("run is unchecked");
}
}
});
swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.print("swim is checked");
} else {
System.out.print("swim is unchecked");
}
}
});
}
}
页面layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RadioGroup
android:id="@+id/genderGroup"
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:checked="true"
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/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
<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" />
</LinearLayout>
strings.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">testbutton</string>
<string name="action_settings">Settings</string>
<string name="hello">Hello World, RadioTest!</string>
<string name="male">male button</string>
<string name="female">female button</string>
<string name="swim">swim</string>
<string name="run">run</string>
<string name="read">read</string>
</resources>