Introduction to the checkbox usage of the Android control series _android

Source: Internet
Author: User
Learning Objectives:

1. How to build a checkbox in Android

2, grasp the common properties of checkbox

3, grasp the checkbox selected State transformation event (listener)

Introduction to checkbox:

CheckBox and Button, is also an ancient control, its advantage is that no user to fill out the specific information, just click, the disadvantage is only "yes" and "no" two situations, but we often use its characteristics, to obtain some information about the user.

As an identity form, often let the user fill out "Whether has been married", obviously let the user to fill out "yes" or "no" is unreasonable, the ideal scenario is to use the following controls:

Checked Status:

Build the layout of the checkbox using the way I did in the button tutorial:

Copy Code code as follows:

<checkbox
Android:id= "@+id/cb"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:checked= "false"
android:text= "Married"
></CheckBox>

Obviously, the checked attribute is one of the most important properties of a checkbox, and there are three ways to change it:

1, the XML declaration 2, the Code dynamic change 3, User touch

Its change will trigger the Oncheckedchange event, and you can use the Oncheckedchangelistener listener to listen to this event, if you are unfamiliar with the listener, you can first learn what I wrote

The button of the Android control series. If you are unfamiliar with how to get controls in your code, you can learn the XML static resources of the Android control series I wrote

The specific anonymous listener method code is as follows:

Copy Code code as follows:

Get checkbox Instance
CheckBox cb = (checkbox) This.findviewbyid (R.ID.CB);
Binding listeners
Cb.setoncheckedchangelistener (New Oncheckedchangelistener () {

@Override
public void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {
TODO auto-generated Method Stub
Toast.maketext (Myactivity.this,
Arg1? " Selected ":" Canceled, Toast.length_long). Show ();
}
});


Summary:

This article describes the simplest example of how to use a checkbox in Android and monitor its selection to change events to achieve the business logic you need.

Related Article

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.