【最后结果显示在一个界面中,但是分开理解】
1、单选按钮(RadioGroup:是单选按钮组和RadioButton)
1)在activity_main.xml中控件是线性布局
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
单选按钮组
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">---------------------->组的方向是垂直方向
组中的第一个按钮
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
组中的第二个按钮
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
2)在strings.xml<?xml version="1.0"encoding="utf-8"?>
HelloWorld,Activity07!
activity07
男人
3)[代码]MainActivity.java代码:publicclassMainActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*///对控件对象进行声明
privateRadioGroupgenderGroup=null;
privateRadioButtonfemaleButton=null;
privateRadioButtonmaleButton=null;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//通过控件的ID来得到代表控件的对象
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(newRadioGroup.OnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
//TODOAuto-generatedmethodstub
if(femaleButton.getId()==checkedId){------------------->如果满足if条件,那么利用Toast类来显示内容为famle的小界面
System.out.println("famale");------------------->为了更方便显示执行过程,写入了System.out.println()Toast.makeText(MainActivity.this,"famle",Toast.LENGTH_SHORT).show();----------->show()方法是让它实现界面显示
}
elseif(maleButton.getId()==checkedId)
{
System.out.println("male");
}
}
});
}
}
【区别Button中的监听方法,其下只是区别的部分代码,是为了方便做出对比】
[代码]java代码:publicclassMainActivityextendsActivity{
privateButtonbutton;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.calculate);
//为按钮对象设置显示值
button.setText("结果");
button.setOnClickListener(newCaculateListener());
}
//编写监听器,是的按下按钮后就给与响应
classCaculateListenerimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
//要使按钮点击后做出什么反应,都在此方法中设置
}
}
}
2、CheckBox:多选按钮【注意:其不存在组的概念】
1)activity_main.xml<?xml version="1.0"encoding="utf-8"?>
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
多选按钮的布局
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"/>
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"/>
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"/>
2)strings.xml<?xml version="1.0"encoding="utf-8"?>
HelloWorld,Activity07!
activity07
swim
run
read
3)[代码]MainActivity.java代码:[仅仅是多选按钮的代码】//为多选按钮添加监听器【为多选按钮中的为一个按钮都要设置监听器】【下篇日记着重介绍CompoundButton类】swimBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){----------->CheckBox继承了CompoundButton类
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){---------->区别单选按钮的监听方法的参数
//TODOAuto-generatedmethodstub
if(isChecked)----------------->若传入的isChecked是真,则就选中该组中的按钮
{----------------->若传入的isChecked是假,则就未选中该组中的按钮
System.out.println("swimischecked");
}
else
{
System.out.println("swimisunchecked");
}
}
});
runBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
//TODOAuto-generatedmethodstub
if(isChecked)
{
System.out.println("runischecked");
}
else
{
System.out.println("runisunchecked");
}
}
});
readBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
//TODOAuto-generatedmethodstub
if(isChecked)
{
System.out.println("readischecked");
}
else
{
System.out.println("readisunchecked");
}
}
});