CheckBox for Android Control (check box control)

Source: Internet
Author: User

First, there are two kinds of states:

Checked state (true), unchecked (false)

Second, the attribute

Android:id = "@+id/checkbox"

Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"

android:checked = "false"

Android:text = "Male"

Three, the code demonstration

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" >    <CheckBoxAndroid:id= "@+id/checkbox1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Basketball"android:checked= "true" />      </LinearLayout>
 PackageCom.muke.textview_edittext;ImportAndroid.os.Bundle;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;ImportAndroid.widget.ImageView;ImportAndroid.widget.ToggleButton;Importandroid.app.Activity; Public classMainactivityextendsactivity{Privatecheckbox checkbox; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //First step: Initialize the control (find the control you want to manipulate)CheckBox =(CheckBox) Findviewbyid (r.id.checkbox1); //Step Two: Check if checkbox is selected by setting the checkbox's Listener eventCheckbox.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                //Step Three: monitor whether the current checkbox is selected by OnCheckedChangedString text = Checkbox.gettext (). toString ();//get the text content of a checkbox                if(isChecked) {System.out.println ("You have selected" +text); }Else{System.out.println ("You canceled" +text);    }            }        }); }        }

  

CheckBox for Android Control (check box control)

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.