Radiogroup radio button usage

Source: Internet
Author: User

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:

<Radiogroup Android: 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:

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 () {@ override public void onclick (view v) {// todo auto-generated method stub RG. clearcheck (); settitle ("radiogroup test") ;}}; private onclicklistener show = new onclicklistener () {@ override public void onclick (view V) {// todo auto-generated method stub if (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 option in the vertex.

RG. new oncheckedchangelistener () {@ override public void oncheckedchanged (radiogroup group, int checkedid) {// todo auto-generated method stub if (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 ();}}});

Source: http://www.pocketdigi.com/20100809/18.html

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.