diff options
author | Scott Main <smain@google.com> | 2012-06-21 17:14:39 -0700 |
---|---|---|
committer | Scott Main <smain@google.com> | 2012-06-21 21:27:30 -0700 |
commit | 50e990c64fa23ce94efa76b9e72df7f8ec3cee6a (patch) | |
tree | 52605cd25e01763596477956963fabcd087054b0 /docs/html/guide/topics/ui/controls/checkbox.jd | |
parent | a2860267cad115659018d636bf9203a644c680a7 (diff) | |
download | frameworks_base-50e990c64fa23ce94efa76b9e72df7f8ec3cee6a.zip frameworks_base-50e990c64fa23ce94efa76b9e72df7f8ec3cee6a.tar.gz frameworks_base-50e990c64fa23ce94efa76b9e72df7f8ec3cee6a.tar.bz2 |
Massive clobber of all HTML files in developer docs for new site design
Change-Id: Idc55a0b368c1d2c1e7d4999601b739dd57f08eb3
Diffstat (limited to 'docs/html/guide/topics/ui/controls/checkbox.jd')
-rw-r--r-- | docs/html/guide/topics/ui/controls/checkbox.jd | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/docs/html/guide/topics/ui/controls/checkbox.jd b/docs/html/guide/topics/ui/controls/checkbox.jd new file mode 100644 index 0000000..35b8f56 --- /dev/null +++ b/docs/html/guide/topics/ui/controls/checkbox.jd @@ -0,0 +1,101 @@ +page.title=Checkboxes +parent.title=Input Controls +parent.link=../controls.html +@jd:body + +<div id="qv-wrapper"> +<div id="qv"> +<h2>In this document</h2> +<ol> + <li><a href="#HandlingEvents">Responding to Click Events</a></li> +</ol> + +<h2>Key classes</h2> +<ol> + <li>{@link android.widget.CheckBox}</li> +</ol> +</div> +</div> + +<p>Checkboxes allow the user to select one or more options from a set. Typically, you should +present each checkbox option in a vertical list.</p> + +<img src="{@docRoot}images/ui/checkboxes.png" alt="" /> + +<p>To create each checkbox option, create a {@link android.widget.CheckBox} in your layout. Because +a set of checkbox options allows the user to select multiple items, each checkbox is managed +separately and you must register a click listener for each one.</p> + +<h2 id="HandlingEvents">Responding to Click Events</h2> + +<p>When the user selects a checkbox, the {@link android.widget.CheckBox} object receives an +on-click event.</p> + +<p>To define the click event handler for a checkbox, add the <code><a +href="/reference/android/R.attr.html#onClick">android:onClick</a></code> attribute to the +<code><CheckBox></code> element in your XML +layout. The value for this attribute must be the name of the method you want to call in response +to a click event. The {@link android.app.Activity} hosting the layout must then implement the +corresponding method.</p> + +<p>For example, here are a couple {@link android.widget.CheckBox} objects in a list:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + <CheckBox android:id="@+id/checkbox_meat" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/meat" + android:onClick="onCheckboxClicked"/> + <CheckBox android:id="@+id/checkbox_cheese" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/cheese" + android:onClick="onCheckboxClicked"/> +</LinearLayout> +</pre> + +<p>Within the {@link android.app.Activity} that hosts this layout, the following method handles the +click event for both checkboxes:</p> + +<pre> +public void onCheckboxClicked(View view) { + // Is the view now checked? + boolean checked = (CheckBox) view).isChecked(); + + // Check which checkbox was clicked + switch(view.getId()) { + case R.id.checkbox_meat: + if (checked) + // Put some meat on the sandwich + else + // Remove the meat + break; + case R.id.checkbox_cheese: + if (checked) + // Cheese me + else + // I'm lactose intolerant + break; + // TODO: Veggie sandwich + } +} +</pre> + +<p>The method you declare in the {@link android.R.attr#onClick android:onClick} attribute +must have a signature exactly as shown above. Specifically, the method must:</p> +<ul> + <li>Be public</li> + <li>Return void</li> + <li>Define a {@link android.view.View} as its only parameter (this will be the {@link +android.view.View} that was clicked)</li> +</ul> + +<p class="note"><strong>Tip:</strong> If you need to change the radio button state +yourself (such as when loading a saved {@link android.preference.CheckBoxPreference}), +use the {@link android.widget.CompoundButton#setChecked(boolean)} or {@link +android.widget.CompoundButton#toggle()} method.</p>
\ No newline at end of file |