public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener{
RadioGroup rp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rp=(RadioGroup) findViewById(R.id.radioGroup1);
rp.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup arg0, int CheckedId) {
// TODO Auto-generated method stub
switch(CheckedId)
{
case R.id.radio0:
Toast.makeText(this, "你是男孩", Toast.LENGTH_SHORT).show();
break;
case R.id.radio1:
Toast.makeText(this, "你是女孩", Toast.LENGTH_SHORT).show();
break;
}
}
}记得别导包android.widget.RadioGroup.OnCheckedChangeListener
<pre name="code" class="html"><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"
tools:context="com.example.xin.radiogroup.MainActivity" >
<RadioGroup
android:id="@+id/radioGroup1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</RelativeLayout>