Familiar with ANDROIDAPI series 2--checkbox and Oncheckedchangelistener

Source: Internet
Author: User

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "android:orientation= "Vertical" > <CheckBox Android:id= "@+id/eatid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Eat"/> <CheckBox Android:id= "@+id/sleepid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Sleeping"/> <CheckBox Android:id= "@+id/dotaid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "DotA"/> <CheckBox Android:id= "@+id/allid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Select All"/></linearlayout>
 PackageCom.njulya.checkbox;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener; Public classMainactivityextendsActivity {PrivateCheckBox Eatbox; PrivateCheckBox Sleepbox; PrivateCheckBox Dotabox; PrivateCheckBox Allbox; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Eatbox=(CheckBox) Findviewbyid (R.id.eatid); Sleepbox=(CheckBox) Findviewbyid (r.id.sleepid); Dotabox=(CheckBox) Findviewbyid (R.id.dotaid); Allbox=(CheckBox) Findviewbyid (r.id.allid); Allboxlistener Alllistener=NewAllboxlistener (); Checkboxlistener Boxlistener=NewCheckboxlistener ();        Eatbox.setoncheckedchangelistener (Boxlistener);        Sleepbox.setoncheckedchangelistener (Boxlistener);        Dotabox.setoncheckedchangelistener (Boxlistener);    Allbox.setoncheckedchangelistener (Alllistener); }        Private classAllboxlistenerImplementsoncheckedchangelistener{@Override Public voidOnCheckedChanged (Compoundbutton arg0,BooleanisChecked) {            if(isChecked) {eatbox.setchecked (true); Sleepbox.setchecked (true); Dotabox.setchecked (true); }Else{eatbox.setchecked (false); Sleepbox.setchecked (false); Dotabox.setchecked (false); }        }            }    Private classCheckboxlistenerImplementsoncheckedchangelistener{@Override Public voidOnCheckedChanged (Compoundbutton checkbox,BooleanisChecked) {            if(Checkbox.getid () = =R.id.eatid) {System.out.println ("Eat"); }            Else if(Checkbox.getid () = =r.id.sleepid) {System.out.println ("Sleep"); }            Else if(Checkbox.getid () = =R.id.dotaid) {System.out.println ("DotA"); }                        if(isChecked) {System.out.println ("Checked"); }            Else{System.out.println ("Unchecked"); } }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

Familiar with ANDROIDAPI series 2--checkbox and Oncheckedchangelistener

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.