<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" android:orientation= "vertical" tools:context= "Com.sadhu.s01_e09_checkbox. Mainactivity$placeholderfragment " > <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/allcheckedbox" android:layoUt_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Select All" ></CheckBox></LinearLayout>
package com.sadhu.s01_e09_checkbox;import android.app.activity;import android.app.fragment; Import android.os.bundle;import android.view.layoutinflater;import android.view.menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.checkbox;import android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import android.view.view.onclicklistener;public class mainactivity extends activity {//defines three variables private checkbox eatbox; private checkbox sleepbox;private checkbox dotabox; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.fragment_main); //found three objects and assigned them to the corresponding variables eatBox = (CheckBox) Findviewbyid (R.id.eatid); sleepBox = (CheckBox) Findviewbyid (r.id.sleepid); dotaBox = (CheckBox) Findviewbyid (r.id.dotaid); //This listener is executed after the selected state has changed, two parameters, one is the object and one is checked. Compoundbutton is a super class checkboxlistener cblistener of a checkbox = new checkboxlistener (); Eatbox.setoncheckedchangelistener (Cblistener); Sleepbox.setoncheckedchangelistener (Cblistener); Dotabox.setoncheckedchangelistener (Cblistener); //defines a listener that changes the state trigger for the Select All button (CheckBox) Findviewbyid (R.id.allcheckedbox). Setoncheckedchangelistener (New oncheckedchangelistener () {@Overridepublic void oncheckedchanged (compoundbutton checkbox, boolean ischecked) {eatbox.setchecked (isChecked); sleepbox.setchecked (isChecked); Dotabox.setchecked (isChecked);});; /* //Instance Click event Listener Class Onboxclicklistener onboxclick = new onboxclicklistener (); //Set the Click event eatbox.setonclicklistener for three objects ( Onboxclick); sleepbox.setonclicklistener (OnBoxClick); dotabox.setonclicklistener (Onboxclick);*/ } //defines a selected state that is changed to perform such class checkboxlistener implements oncheckedchangelistener {@Overridepublic void OnCheckedChanged (compoundbutton buttonview, boolean ischecked) {if (buttonView.getId () = = R.id.eatid) {System.out.println ("Eatbox");} Else if (Buttonview.getid () ==r.id.sleepid) {System.out.println ("Sleepbox");} Else if (Buttonview.getid () ==r.id.dotaid) {System.out.println ("Dotabox");} if (isChecked) {System.out.println ("checked");} Else{system.out.println ("Unchecked");}} } //Define a click event Listener class /* class OnBoxClickListener implements onclicklistener { @Override public void onclick (View view) { CheckBox box = (CheckBox) view; if (Box.getid () ==r.id.eatid) { system.out.println ("Eatbox"); } else if (Box.getid () ==r.id.sleepid) { System.out.println ("Sleepbox"); } else if (Box.getid () = = R.id.dotaid) { system.out.println ("DotaBox"); } if (box.ischecked ()) { System.out.println ("checked"); } else { system.out.println ("unchecked"); } } }*/ &nbSP; @Override public boolean oncreateoptionsmenu (Menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem item) { // Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in Androidmanifest.xml. int id = item.getitemid (); if (id == r.id.action_settings) { return true; } return Super.onoptionsitemselected (item); } /** * A placeholder fragment containing a simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment () { } @Override public view oncreateview (layoutinflater inflater, viewgroup container, bundle savedinstancestate) { View rootView = Inflater.inflate (R.layout.fragment_main, container, false); return rootview; } }}
The Multi-select button checkbox is simple enough to provide a multi-select function for the user, with two listeners, one with the Onclicklistener-click Control triggering the Listener, One is the listener (Oncheckedchangelistener) that executes after the button selection has changed.
Use of the Android Multi-select button checkbox