MainActivity. java
Package com. jk. test; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. radioButton; import android. widget. radioGroup; public class MainActivity extends Activity {private RadioButton eatButton; private RadioButton sleepButton; private RadioButton dotaButton; private RadioGroup groupButton; protected void onCreate (Bundle extension) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); eatButton = (RadioButton) findViewById (R. id. eatButtonid); sleepButton = (RadioButton) findViewById (R. id. sleepButtonid); dotaButton = (RadioButton) findViewById (R. id. dotaButtonid); RadioGroupListener listener1 = new RadioGroupListener (); groupButton. setOnCheckedChangeListener (listener1); RadioButtonListener listener2 = new RadioButtonListener (); eatButton. setOnCheckedChangeListener (listener2); sleepButton. setOnCheckedChangeListener (listener2); dotaButton. setOnCheckedChangeListener (listener2);} class RadioButtonListener implements OnCheckedChangeListener {public void onCheckedChanged (CompoundButton v, boolean isChecked) {System. out. println (v. getId () + "isChecked --->" + isChecked);} class RadioGroupListener implements android. widget. radioGroup. onCheckedChangeListener {public void onCheckedChanged (RadioGroup v, int checkedId) {if (checkedId = eatButton. getId () System. out. println ("eatButton selected"); else if (checkedId = sleepButton. getId () System. out. println ("selected sleepButton"); else if (checkedId = dotaButton. getId () System. out. println ("dotaButton selected") ;}} 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 ;}}
Activity_main.xml
<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <RadioGroup android: id =" @ + id/radioGroupId "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: orientation = "horizontal"> <RadioButton android: id = "@ + id/eatButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "dinner"/> <RadioButton android: id = "@ + id/sleepButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sleeping"/> <RadioButton android: id = "@ + id/dotaButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "dota"/> </RadioGroup> </LinearLayout>