Address: http://www.pocketdigi.com/20100809/18.html
Use the single-choice button of radiogroup.
Click the radiogroup test button to display the selection result in the title bar. Click Clear to clear the selection.
The code below is main. xml:
1234567891011121314151617181920212223242526272829303132 |
<Radiogroupandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "vertical" Android: checkedbutton = "@ + ID/B1" Android: id = "@ + ID/rg"> <! -- Select B1 by default --> <radiobutton Android: text = "1" Android: Id = "@ + ID/B1"/> <radiobutton Android: text = "2" Android: id = "@ + ID/B2"/> <radiobutton Android: text = "3" Android: id = "@ + ID/B3"/> </radiogroup> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: id = "@ + ID/Show" Android: text = "radiogroup test"/> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: id = "@ + ID/clear" Android: text = "clear"/> |
Program code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
Package COM. pocketdigi; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. radiobutton; import android. widget. radiogroup; public class main extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); settitle ("radiogroup test"); setcontentview (R. layout. main); rgdemo ();} radiogroup RG; radiobutton B1; radiobutton B2; radiobutton B3; Public void rgdemo () {Rg = (radiogroup) findviewbyid (R. id. RG); b1 = (radiobutton) findviewbyid (R. id. b1); b2 = (radiobutton) findviewbyid (R. id. b2); B3 = (radiobutton) findviewbyid (R. id. b3); button CLR = (button) findviewbyid (R. id. clear); CLR. setonclicklistener (clear); button echo = (button) findviewbyid (R. id. show); echo. setonclicklistener (show);} private button. onclicklistener clear = new onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubrg. clearcheck (); settitle ("radiogroup test") ;}}; private onclicklistener show = new onclicklistener () {@ overridepublic void onclick (view V) {// todo auto-generated method stubif (b1.ischecked () {settitle ("1") ;}if (b2.ischecked () {settitle ("2 ");} if (b3.ischecked () {settitle ("3 ");}}};} |
Radiogroup has an oncheckchangelistener listener that can capture click events through the oncheckedchanged method of the listener. The oncheckedchanged method will pass in an int type checkedid. You can compare the passed checkedid and radiobutton ID, to determine the options in the vertex.
123456789101112131415161718 19 |
RG. listener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (radiogroup group, int checkedid) {// todo auto-generated method stubif (checkedid = b1.getid () {toast. maketext (main. this, "B1 selected", toast. length_long ). show () ;}if (checkedid = b2.getid () {toast. maketext (main. this, "B2 selected", toast. length_long ). show () ;}if (checkedid = b3.getid () {toast. maketext (main. this, "B3 selected", toast. length_long ). show ();}}}); |