Skip to content

Commit 4859a61

Browse files
asolovayAndroid (Google) Code Review
authored andcommitted
Merge "docs: Clarified how to find out when a switch is toggled" into mnc-preview-docs
2 parents 2b85610 + 07e7021 commit 4859a61

2 files changed

Lines changed: 18 additions & 76 deletions

File tree

core/java/android/widget/Switch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related
5858
* setTypeface() methods control the typeface and style of label text, whereas the
5959
* {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and
60-
* the related seSwitchTypeface() methods control that of the thumb.
60+
* the related setSwitchTypeface() methods control that of the thumb.
6161
*
6262
* <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
6363
* guide.</p>

docs/html/guide/topics/ui/controls/togglebutton.jd

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ page.tags=switch,togglebutton
66
<div id="qv">
77
<h2>In this document</h2>
88
<ol>
9-
<li><a href="#HandlingEvents">Responding to Click Events</a>
10-
<ol>
11-
<li><a href="#ClickListener">Using an OnCheckedChangeListener</a></li>
12-
</ol>
9+
<li>
10+
<a href="#ClickListener">Responding to Button Presses</a>
1311
</li>
1412
</ol>
1513
<h2>Key classes</h2>
1614
<ol>
1715
<li>{@link android.widget.ToggleButton}</li>
1816
<li>{@link android.widget.Switch}</li>
17+
<li>{@link android.widget.CompoundButton}</li>
1918
</ol>
2019
</div>
2120
</div>
@@ -26,6 +25,12 @@ page.tags=switch,togglebutton
2625
object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that
2726
provides a slider control, which you can add with a {@link android.widget.Switch} object.</p>
2827

28+
<p>
29+
If you need to change a button's state yourself, you can use the {@link
30+
android.widget.CompoundButton#setChecked CompoundButton.setChecked()} or
31+
{@link android.widget.CompoundButton#toggle CompoundButton.toggle()} methods.
32+
</p>
33+
2934
<div style="float:left;width:200px">
3035
<img src="{@docRoot}images/ui/togglebutton.png" alt="" />
3136
<p class="img-caption"><em>Toggle buttons</em></p>
@@ -36,78 +41,15 @@ provides a slider control, which you can add with a {@link android.widget.Switch
3641
<p class="img-caption"><em>Switches (in Android 4.0+)</em></p>
3742
</div>
3843

39-
<p style="clear:left">The {@link android.widget.ToggleButton} and {@link android.widget.Switch}
40-
controls are subclasses of {@link android.widget.CompoundButton} and function in the same manner, so
41-
you can implement their behavior the same way.</p>
42-
43-
<h2 id="HandlingEvents">Responding to Click Events</h2>
44-
45-
<p>When the user selects a {@link android.widget.ToggleButton} and {@link android.widget.Switch},
46-
the object receives an on-click event.</p>
47-
48-
<p>To define the click event handler, add the <code><a
49-
href="/reference/android/R.attr.html#onClick">android:onClick</a></code> attribute to the
50-
<code>&lt;ToggleButton&gt;</code> or <code>&lt;Switch&gt;</code> element in your XML
51-
layout. The value for this attribute must be the name of the method you want to call in response
52-
to a click event. The {@link android.app.Activity} hosting the layout must then implement the
53-
corresponding method.</p>
54-
55-
<p>For example, here's a {@link android.widget.ToggleButton} with the <code><a
56-
href="/reference/android/R.attr.html#onClick">android:onClick</a></code> attribute:</p>
57-
58-
<pre>
59-
&lt;ToggleButton
60-
android:id="@+id/togglebutton"
61-
android:layout_width="wrap_content"
62-
android:layout_height="wrap_content"
63-
android:textOn="Vibrate on"
64-
android:textOff="Vibrate off"
65-
android:onClick="onToggleClicked"/>
66-
</pre>
67-
68-
<p>Within the {@link android.app.Activity} that hosts this layout, the following method handles the
69-
click event:</p>
70-
71-
<pre>
72-
public void onToggleClicked(View view) {
73-
// Is the toggle on?
74-
boolean on = ((ToggleButton) view).isChecked();
75-
76-
if (on) {
77-
// Enable vibrate
78-
} else {
79-
// Disable vibrate
80-
}
81-
}
82-
</pre>
83-
84-
<p>The method you declare in the {@link android.R.attr#onClick android:onClick} attribute
85-
must have a signature exactly as shown above. Specifically, the method must:</p>
86-
<ul>
87-
<li>Be public</li>
88-
<li>Return void</li>
89-
<li>Define a {@link android.view.View} as its only parameter (this will be the {@link
90-
android.view.View} that was clicked)</li>
91-
</ul>
92-
93-
<p class="note"><strong>Tip:</strong> If you need to change the state
94-
yourself,
95-
use the {@link android.widget.CompoundButton#setChecked(boolean)} or {@link
96-
android.widget.CompoundButton#toggle()} method to change the state.</p>
97-
98-
99-
100-
<h3 id="ClickListener">Using an OnCheckedChangeListener</h3>
101-
102-
<p>You can also declare a click event handler programmatically rather than in an XML layout. This
103-
might be necessary if you instantiate the {@link android.widget.ToggleButton} or {@link
104-
android.widget.Switch} at runtime or you need to
105-
declare the click behavior in a {@link android.app.Fragment} subclass.</p>
44+
<h2 id="ClickListener">Responding to Button Presses</h2>
10645

107-
<p>To declare the event handler programmatically, create an {@link
108-
android.widget.CompoundButton.OnCheckedChangeListener} object and assign it to the button by calling
109-
{@link
110-
android.widget.CompoundButton#setOnCheckedChangeListener}. For example:</p>
46+
<p>
47+
To detect when the user activates the button or switch, create an {@link
48+
android.widget.CompoundButton.OnCheckedChangeListener} object and assign it
49+
to the button by calling {@link
50+
android.widget.CompoundButton#setOnCheckedChangeListener
51+
setOnCheckedChangeListener()}. For example:
52+
</p>
11153

11254
<pre>
11355
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);

0 commit comments

Comments
 (0)