Configuration of XML files in Radiogroup <RadioGroup android:id= "@+id/radiogroupid" Android:layout_width= "wrap_content" android:layout_height= "wrap _content " android:orientation=" Horizontal " > <RadioButton android:id= "@+id/femalebutton" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "female" /> <radiobutton android:id= "@+id/malebutton" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Male" /> Code snippets in activity in </radiogroup> //radiogroup public class MainActivity extends Activity {private RadioGroup radiogroup;private radiobutton femalebutton,malebutton; @Override protected voId oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_main); radiogroup= (Radiogroup) findViewById ( R.ID.RADIOGROUPID); femalebutton= (RadioButton) findViewById ( R.id.femalebutton); malebutton= (RadioButton) findViewById ( R.id.malebutton); radiogrouplis r=new radiogrouplis () ; radiogroup.setoncheckedchangelistener (R); } class RadioGroupLis implements oncheckedchangelistener{@Overridepublic void oncheckedchanged (Radiogroup group, int checkedid) {if (checkedid==femalebutton.getid ()) {Toast.maketext (Getapplicationcontext (), "Check female", toast.length_long). Show ( );} Else if (Checkedid==malebutton.getid ()) {Toast.maketext (Getapplicationcontext (), "Check male", Toast.length_short). Show ();}} } XML files in checkbox <CheckBox android:id= "@+id/eatid" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Eat" /><checkbox android:id= "@+id/playid" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Game" /><checkbox&nBsp; android:id= "@+id/sleepid" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" Sleep " /> // Activity files in the checkbox public class MainActivity extends activity {private checkbox eatbox,sleepbox,playbox; @ Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); eatbox= (checkbox) Findviewbyid (R.id.eatid); sleepbox= (checkbox) Findviewbyid (r.id.sleepid); playbox= (CheckBox) Findviewbyid (R.id.playid); onboxlis listener=new onboxlis (); Eatbox.setonclicklistener (Listener); Sleepbox.setonclicklistener (Listener); Playbox.setonclicklistener (listener); } How to use //onclicklistener class onboxlis implements onclicklistener{@Overridepublic void onclick (view v) { checkbox box= (CheckBox) v;if (box.ischecked ()) {Toast.maketext (Getapplicationcontext (), "selected", toast.length_short). Show ();} Else{toast.maketext (Getapplicationcontext (), "not selected", toast.length_long). Show ();}} }
This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1658339
Application of Radionbutton and checkbox in Android