From 07e7021e062dd3579f662502b15c0682c4672cfe Mon Sep 17 00:00:00 2001 From: Andrew Solovay Date: Wed, 8 Jul 2015 12:49:21 -0700 Subject: docs: Clarified how to find out when a switch is toggled Per Chris, the existing doc was incorrect: you *can't* check for a button press and a switch flip the same way. (The button triggers a click event, but the switch does not.) Chris suggested that we just remove the reference to onClick and suggest using a listener for both kinds of buttons (ToggleButton and Switch). Also pulled one note out of a section where it didn't fit (the bit about changing a button/switch's state programmatically didn't have much to do with listening for clicks) and put it at the top, and I fixed a Javadoc typo for a relevant class that I happened to notice. See first comment for doc stage location. bug: 20625504 Change-Id: I9c8975111381e5b169f6a61454ef3a93da635759 --- docs/html/guide/topics/ui/controls/togglebutton.jd | 92 ++++------------------ 1 file changed, 17 insertions(+), 75 deletions(-) (limited to 'docs/html/guide/topics/ui/controls/togglebutton.jd') diff --git a/docs/html/guide/topics/ui/controls/togglebutton.jd b/docs/html/guide/topics/ui/controls/togglebutton.jd index 09af516..e0549ec 100644 --- a/docs/html/guide/topics/ui/controls/togglebutton.jd +++ b/docs/html/guide/topics/ui/controls/togglebutton.jd @@ -6,16 +6,15 @@ page.tags=switch,togglebutton

In this document

    -
  1. Responding to Click Events -
      -
    1. Using an OnCheckedChangeListener
    2. -
    +
  2. + Responding to Button Presses

Key classes

  1. {@link android.widget.ToggleButton}
  2. {@link android.widget.Switch}
  3. +
  4. {@link android.widget.CompoundButton}
@@ -26,6 +25,12 @@ page.tags=switch,togglebutton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a {@link android.widget.Switch} object.

+

+ If you need to change a button's state yourself, you can use the {@link + android.widget.CompoundButton#setChecked CompoundButton.setChecked()} or + {@link android.widget.CompoundButton#toggle CompoundButton.toggle()} methods. +

+

Toggle buttons

@@ -36,78 +41,15 @@ provides a slider control, which you can add with a {@link android.widget.Switch

Switches (in Android 4.0+)

-

The {@link android.widget.ToggleButton} and {@link android.widget.Switch} -controls are subclasses of {@link android.widget.CompoundButton} and function in the same manner, so -you can implement their behavior the same way.

- -

Responding to Click Events

- -

When the user selects a {@link android.widget.ToggleButton} and {@link android.widget.Switch}, -the object receives an on-click event.

- -

To define the click event handler, add the android:onClick attribute to the -<ToggleButton> or <Switch> 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.

- -

For example, here's a {@link android.widget.ToggleButton} with the android:onClick attribute:

- -
-<ToggleButton 
-    android:id="@+id/togglebutton"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:textOn="Vibrate on"
-    android:textOff="Vibrate off"
-    android:onClick="onToggleClicked"/>
-
- -

Within the {@link android.app.Activity} that hosts this layout, the following method handles the -click event:

- -
-public void onToggleClicked(View view) {
-    // Is the toggle on?
-    boolean on = ((ToggleButton) view).isChecked();
-    
-    if (on) {
-        // Enable vibrate
-    } else {
-        // Disable vibrate
-    }
-}
-
- -

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:

- - -

Tip: If you need to change the state -yourself, -use the {@link android.widget.CompoundButton#setChecked(boolean)} or {@link -android.widget.CompoundButton#toggle()} method to change the state.

- - - -

Using an OnCheckedChangeListener

- -

You can also declare a click event handler programmatically rather than in an XML layout. This -might be necessary if you instantiate the {@link android.widget.ToggleButton} or {@link -android.widget.Switch} at runtime or you need to -declare the click behavior in a {@link android.app.Fragment} subclass.

+

Responding to Button Presses

-

To declare the event handler programmatically, create an {@link -android.widget.CompoundButton.OnCheckedChangeListener} object and assign it to the button by calling -{@link -android.widget.CompoundButton#setOnCheckedChangeListener}. For example:

+

+ To detect when the user activates the button or switch, create an {@link + android.widget.CompoundButton.OnCheckedChangeListener} object and assign it + to the button by calling {@link + android.widget.CompoundButton#setOnCheckedChangeListener + setOnCheckedChangeListener()}. For example: +

 ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
-- 
cgit v1.1