The Check Box Control

A check-box control plays a part in virtually all widget toolkits. HTML, JFC, and JSF all support the concept of a check box. The check-box control is a two-state button that allows the user to toggle its state.

In Android, you can create a check box by creating an instance of android.widget. CheckBox. See Listing 4-14 and Figure 4-4.

Listing 4-14. Creating Check Boxes

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<CheckBox android:text="Chicken"

android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:text="Fish"

android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:text="Steak"

android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

Figure 4-4. Using the CheckBox control

You manage the state of a check box by calling setChecked() or toggle(). You can obtain the state by calling isChecked().

If you need to implement specific logic when a check box is checked or unchecked, you can register for the on-checked event by calling setOnCheckedChangeListener() with an implementation of the OnCheckedChangeListener interface. You'll then have to implement the onCheckedChanged() method, which will be called when the check box is checked or unchecked.

0 0

Post a comment