package com.example.activity07;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
 
public class MainActivity extends Activity {
//对控件对象进行声明
//我操
private
RadioButton
femaleButton
=
null;private
RadioButton
maleButton
=
null;我特么的写成RadioGroup
private
RadioGroup genderGroup
=
null;
private
RadioButton
femaleButton
=
null;
private
RadioButton
maleButton
=
null;
private
CheckBox
swimBox
=
null;
private
CheckBox
runBox
=
null;
private
CheckBox
readBox
=
null;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过控件的ID来得到代表控件的对象
//卧槽,坑爹啊,(RadioButton)写成(RadioGroup),浪费了半天时间,坑爹啊!
genderGroup
=
(RadioGroup)findViewById(R.id.genderGroup);
femaleButton
=
(RadioButton)findViewById(R.id.femaleButton);
maleButton
=
(RadioButton)findViewById(R.id.maleButton);
swimBox
=
(CheckBox)findViewById(R.id.swim);
runBox
=
(CheckBox)findViewById(R.id.run);
readBox
=
(CheckBox)findViewById(R.id.read);
//为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器不同
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId()==
checkedId){
System.out.println("famale");
Toast.makeText(MainActivity.this, "女性", Toast.LENGTH_SHORT).show();
}
else
if(maleButton.getId()==checkedId){
System.out.println("male");
Toast.makeText(MainActivity.this, "男性", Toast.LENGTH_SHORT).show();
}
}
});
//为多选按钮增加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("swim is checked");
}
else{
System.out.println("swim is unchecked");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("run is checked");
}
else{
System.out.println("run is unchecked");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("read is checked");
}
else{
System.out.println("read is unchecked");
}
}
});
}
@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;
}
 
}