Application of Radionbutton and checkbox in Android

Source: Internet
Author: User

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

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.