布局文件
<RelativeLayout 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: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="com.example.day02_2.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rg"
android:text="读书"/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb"
android:text="画画"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb1"
android:text="游泳"/>
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cb2"
android:text="" />
</RelativeLayout>
主函数代码
package com.example.day02_2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private RadioGroup rg = null;
private RadioButton rb = null;
private RadioButton rb1 = null;
private CheckBox cb = null;
private CheckBox cb1 = null;
private CheckBox cb2 = null;
private TextView tv1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.rg);
rb = (RadioButton) findViewById(R.id.rb);
rb1 = (RadioButton) findViewById(R.id.rb1);
cb = (CheckBox) findViewById(R.id.cb);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
tv1 = (TextView) findViewById(R.id.tv1);
// 给RadioGroup添加监听事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO 自动生成的方法存根
if (rb.getId() == checkedId) {
tv1.setText("您选择了男");
} else if (rb1.getId() == checkedId) {
tv1.setText("您选择了女");
}
}
});
// CheckBox需要给每一个都添加监听事件
// 给第一个CheckBox注册监听事件
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
if (isChecked) {
tv1.setText("读书");
} else {
}
}
});
// 给第二个CheckBox注册监听事件
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
if (isChecked) {
tv1.setText("画画");
} else {
}
}
});
//给第三个CheckBox注册监听事件
cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
if (isChecked) {
tv1.setText("游泳");
} else {
}
}
});
}
}