Activity:
package com.example.relativelayout; 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 RadioGroup rg=null; private RadioButton female=null; private RadioButton male=null; private CheckBox swim=null; private CheckBox run=null; private CheckBox badminton=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg=(RadioGroup)findViewById(R.id.radiogroup); female=(RadioButton)findViewById(R.id.female); male=(RadioButton)findViewById(R.id.male); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId==female.getId()){ System.out.println("female"); Toast.makeText(MainActivity.this, "female", Toast.LENGTH_LONG).show(); }else if(checkedId==male.getId()){ System.out.println("male"); } } }); swim=(CheckBox)findViewById(R.id.swim); run=(CheckBox)findViewById(R.id.run); badminton=(CheckBox)findViewById(R.id.badminton); swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("swim is checked"); }else{ System.out.println("swim is not checked"); } } }); run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("run is checked"); }else{ System.out.println("run is not checked"); } } }); badminton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("badminton is checked"); }else{ System.out.println("badminton is not checked"); } } }); } @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; } }
在布局文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioGroup android:id="@+id/radiogroup" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/female" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/male" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/male" /> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/swim" ></CheckBox> <CheckBox android:id="@+id/run" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/run" ></CheckBox> <CheckBox android:id="@+id/badminton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/badminton" ></CheckBox> </LinearLayout>