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 
     23 import com.android.internal.R;
     24 
     25 
     26 /**
     27  * <p>
     28  * A radio button is a two-states button that can be either checked or
     29  * unchecked. When the radio button is unchecked, the user can press or click it
     30  * to check it. However, contrary to a {@link android.widget.CheckBox}, a radio
     31  * button cannot be unchecked by the user once checked.
     32  * </p>
     33  *
     34  * <p>
     35  * Radio buttons are normally used together in a
     36  * {@link android.widget.RadioGroup}. When several radio buttons live inside
     37  * a radio group, checking one radio button unchecks all the others.</p>
     38  * </p>
     39  *
     40  * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-formstuff.html">Form Stuff
     41  * tutorial</a>.</p>
     42  *
     43  * <p><strong>XML attributes</strong></p>
     44  * <p>
     45  * See {@link android.R.styleable#CompoundButton CompoundButton Attributes},
     46  * {@link android.R.styleable#Button Button Attributes},
     47  * {@link android.R.styleable#TextView TextView Attributes},
     48  * {@link android.R.styleable#View View Attributes}
     49  * </p>
     50  */
     51 public class RadioButton extends CompoundButton {
     52 
     53     public RadioButton(Context context) {
     54         this(context, null);
     55     }
     56 
     57     public RadioButton(Context context, AttributeSet attrs) {
     58         this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
     59     }
     60 
     61     public RadioButton(Context context, AttributeSet attrs, int defStyle) {
     62         super(context, attrs, defStyle);
     63     }
     64 
     65     /**
     66      * {@inheritDoc}
     67      * <p>
     68      * If the radio button is already checked, this method will not toggle the radio button.
     69      */
     70     @Override
     71     public void toggle() {
     72         // we override to prevent toggle when the radio is already
     73         // checked (as opposed to check boxes widgets)
     74         if (!isChecked()) {
     75             super.toggle();
     76         }
     77     }
     78 
     79     @Override
     80     public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
     81         super.onPopulateAccessibilityEvent(event);
     82         if (isChecked()) {
     83             event.getText().add(mContext.getString(R.string.radiobutton_selected));
     84         } else {
     85             event.getText().add(mContext.getString(R.string.radiobutton_not_selected));
     86         }
     87     }
     88 }
     89