一个radioGroup中包含多个radioButton这些按钮就是一组,一组中只能有一个按钮被选中,可以定义多组RadioGroup,可以为RadioGroup控件设置OnCheckedChangeListener监听器,可以为RadioButton控件也设置 个OnCheckedChangeListener他们监听器是在不同的包下。
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.sadhu.s01_e10_radio.MainActivity$PlaceholderFragment" >
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioId1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radioId2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
package com.sadhu.s01_e10_radio;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioButton radioBtton1;
private RadioButton radioButton2;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioBtton1 = (RadioButton)findViewById(R.id.radioId1);
radioButton2 = (RadioButton)findViewById(R.id.radioId2);
radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
//实例化单选按钮组监听器
RadioGroupListener listener = new RadioGroupListener();
//实例化单选按钮监听器
OnCheckChangeListener radioListener = new OnCheckChangeListener();
//为控件设置监听器
radioGroup.setOnCheckedChangeListener(listener);
radioBtton1.setOnCheckedChangeListener(radioListener);
radioButton2.setOnCheckedChangeListener(radioListener);
}
//为单选按钮定义一个监听器
//android.widget.CompoundButton
class OnCheckChangeListener implements android.widget.CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton boxButton, boolean isChecked) {
if(isChecked){
System.out.println("checked:"+boxButton.getId());
}
else
{
System.out.println("unchecked:"+boxButton.getId());
}
}
}
//RadioGroup的监听器,两个参数,一个是哪个对象组被选中了,后面是对象里面的哪个对象被选中了。
//android.widget.RadioGroup.OnCheckedChangeListener;
class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if(checkedId == radioBtton1.getId()){
System.out.println("选中了男!");
}
else if(checkedId == radioButton2.getId()){
System.out.println("选中了女!");
}
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}