Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.widget;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.view.accessibility.AccessibilityEvent;
     22 import android.view.accessibility.AccessibilityNodeInfo;
     23 
     24 
     25 /**
     26  * <p>
     27  * A radio button is a two-states button that can be either checked or
     28  * unchecked. When the radio button is unchecked, the user can press or click it
     29  * to check it. However, contrary to a {@link android.widget.CheckBox}, a radio
     30  * button cannot be unchecked by the user once checked.
     31  * </p>
     32  *
     33  * <p>
     34  * Radio buttons are normally used together in a
     35  * {@link android.widget.RadioGroup}. When several radio buttons live inside
     36  * a radio group, checking one radio button unchecks all the others.</p>
     37  * </p>
     38  *
     39  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/radiobutton.html">Radio Buttons</a>
     40  * guide.</p>
     41  *
     42  * <p><strong>XML attributes</strong></p>
     43  * <p>
     44  * See {@link android.R.styleable#CompoundButton CompoundButton Attributes},
     45  * {@link android.R.styleable#Button Button Attributes},
     46  * {@link android.R.styleable#TextView TextView Attributes},
     47  * {@link android.R.styleable#View View Attributes}
     48  * </p>
     49  */
     50 public class RadioButton extends CompoundButton {
     51 
     52     public RadioButton(Context context) {
     53         this(context, null);
     54     }
     55 
     56     public RadioButton(Context context, AttributeSet attrs) {
     57         this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
     58     }
     59 
     60     public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
     61         this(context, attrs, defStyleAttr, 0);
     62     }
     63 
     64     public RadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
     65         super(context, attrs, defStyleAttr, defStyleRes);
     66     }
     67 
     68     /**
     69      * {@inheritDoc}
     70      * <p>
     71      * If the radio button is already checked, this method will not toggle the radio button.
     72      */
     73     @Override
     74     public void toggle() {
     75         // we override to prevent toggle when the radio is already
     76         // checked (as opposed to check boxes widgets)
     77         if (!isChecked()) {
     78             super.toggle();
     79         }
     80     }
     81 
     82     @Override
     83     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     84         super.onInitializeAccessibilityEvent(event);
     85         event.setClassName(RadioButton.class.getName());
     86     }
     87 
     88     @Override
     89     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
     90         super.onInitializeAccessibilityNodeInfo(info);
     91         info.setClassName(RadioButton.class.getName());
     92     }
     93 }
     94