The checkbox check button is a special type of button with two statuses, which can be selected or not selected. You can now define the multiple-choice button in the layout file, and then perform event monitoring setoncheckedchangelistener on each multiple-choice button. The ischecked is used to determine whether the option is selected.
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/theme"/> <br/> <checkbox <br/> Android: Id = "@ + ID/Apple" <br/> Android: TEXT = "@ string/Apple" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> <br/> <checkbox <br/> Android: Id = "@ + ID/banana" <br/> Android: TEXT = "@ string/banana" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> </linearlayout>
radiogroupactivity. Java
Package android. activity; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. checkbox; <br/> Import android. widget. compoundbutton; <br/> Import android. widget. toast; </P> <p> public class radiogroupactivity extends activity {<br/>/** called when the activity is first created. */<br/> private checkbox applecheck = NULL; <br/> private checkbox bananacheck = NULL; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // obtain the object representing the control using the control id <br/> applecheck = (checkbox) findviewbyid (R. id. apple); <br/> bananacheck = (checkbox) findviewbyid (R. id. banana); <br/> // set the listener for the radiogroup. The Listener here is different from the listener for the button control. <br/> applecheck. setoncheckedchangelistener (New compoundbutton. oncheckedchangelistener () {</P> <p> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> // todo auto-generated method stub <br/> If (ischecked) {<br/> toast. maketext (radiogroupactivity. this, "congratulations, there are a lot of apples left", toast. length_long ). show (); <br/>}< br/>}); </P> <p> bananacheck. setoncheckedchangelistener (New compoundbutton. oncheckedchangelistener () {</P> <p> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> // todo auto-generated method stub <br/> If (ischecked) {<br/> toast. maketext (radiogroupactivity. this, "Congratulations, you have a lot of bananas left", toast. length_long ). show (); <br/>}< br/>}); <br/>}< br/>}