• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.accessibility.AccessibilityNodeInfo;
23 import android.widget.RemoteViews.RemoteView;
24 
25 import com.android.internal.R;
26 
27 
28 /**
29  * <p>
30  * A radio button is a two-states button that can be either checked or
31  * unchecked. When the radio button is unchecked, the user can press or click it
32  * to check it. However, contrary to a {@link android.widget.CheckBox}, a radio
33  * button cannot be unchecked by the user once checked.
34  * </p>
35  *
36  * <p>
37  * Radio buttons are normally used together in a
38  * {@link android.widget.RadioGroup}. When several radio buttons live inside
39  * a radio group, checking one radio button unchecks all the others.</p>
40  * </p>
41  *
42  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/radiobutton.html">Radio Buttons</a>
43  * guide.</p>
44  *
45  * <p><strong>XML attributes</strong></p>
46  * <p>
47  * See {@link android.R.styleable#CompoundButton CompoundButton Attributes},
48  * {@link android.R.styleable#Button Button Attributes},
49  * {@link android.R.styleable#TextView TextView Attributes},
50  * {@link android.R.styleable#View View Attributes}
51  * </p>
52  */
53 @RemoteView
54 public class RadioButton extends CompoundButton {
55 
RadioButton(Context context)56     public RadioButton(Context context) {
57         this(context, null);
58     }
59 
RadioButton(Context context, AttributeSet attrs)60     public RadioButton(Context context, AttributeSet attrs) {
61         this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
62     }
63 
RadioButton(Context context, AttributeSet attrs, int defStyleAttr)64     public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
65         this(context, attrs, defStyleAttr, 0);
66     }
67 
RadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)68     public RadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
69         super(context, attrs, defStyleAttr, defStyleRes);
70     }
71 
72     /**
73      * {@inheritDoc}
74      * <p>
75      * If the radio button is already checked, this method will not toggle the radio button.
76      */
77     @Override
toggle()78     public void toggle() {
79         // we override to prevent toggle when the radio is already
80         // checked (as opposed to check boxes widgets)
81         if (!isChecked()) {
82             super.toggle();
83         }
84     }
85 
86     @Override
getAccessibilityClassName()87     public CharSequence getAccessibilityClassName() {
88         return RadioButton.class.getName();
89     }
90 
91     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)92     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
93         super.onInitializeAccessibilityNodeInfo(info);
94         if (getParent() instanceof RadioGroup) {
95             RadioGroup radioGroup = (RadioGroup) getParent();
96             if (radioGroup.getOrientation() == LinearLayout.HORIZONTAL) {
97                 info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain(0, 1,
98                         radioGroup.getIndexWithinVisibleButtons(this), 1, false, isChecked()));
99             } else {
100                 info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain(
101                         radioGroup.getIndexWithinVisibleButtons(this), 1, 0, 1,
102                         false, isChecked()));
103             }
104         }
105     }
106 
107     /** @hide **/
108     @Override
109     @NonNull
getButtonStateDescription()110     protected CharSequence getButtonStateDescription() {
111         if (isChecked()) {
112             return getResources().getString(R.string.selected);
113         } else {
114             return getResources().getString(R.string.not_selected);
115         }
116     }
117 }
118