diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index f45e7505f41b1..e672fc38e65c6 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -62,7 +62,7 @@ import com.android.internal.R; * {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related * setTypeface() methods control the typeface and style of label text, whereas the * {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and - * the related seSwitchTypeface() methods control that of the thumb. + * the related setSwitchTypeface() methods control that of the thumb. * *

See the Toggle Buttons * guide.

diff --git a/docs/html/guide/topics/ui/controls/togglebutton.jd b/docs/html/guide/topics/ui/controls/togglebutton.jd index 09af516a324eb..e0549ecb28f43 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 Button Presses

-

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.

- -

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);