The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 21 | import android.view.accessibility.AccessibilityNodeInfo; |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * <p> |
| 26 | * A radio button is a two-states button that can be either checked or |
| 27 | * unchecked. When the radio button is unchecked, the user can press or click it |
| 28 | * to check it. However, contrary to a {@link android.widget.CheckBox}, a radio |
| 29 | * button cannot be unchecked by the user once checked. |
| 30 | * </p> |
| 31 | * |
| 32 | * <p> |
| 33 | * Radio buttons are normally used together in a |
| 34 | * {@link android.widget.RadioGroup}. When several radio buttons live inside |
| 35 | * a radio group, checking one radio button unchecks all the others.</p> |
| 36 | * </p> |
| 37 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 38 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/radiobutton.html">Radio Buttons</a> |
| 39 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 40 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | * <p><strong>XML attributes</strong></p> |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 42 | * <p> |
| 43 | * See {@link android.R.styleable#CompoundButton CompoundButton Attributes}, |
| 44 | * {@link android.R.styleable#Button Button Attributes}, |
| 45 | * {@link android.R.styleable#TextView TextView Attributes}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * {@link android.R.styleable#View View Attributes} |
| 47 | * </p> |
| 48 | */ |
| 49 | public class RadioButton extends CompoundButton { |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | public RadioButton(Context context) { |
| 52 | this(context, null); |
| 53 | } |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | public RadioButton(Context context, AttributeSet attrs) { |
| 56 | this(context, attrs, com.android.internal.R.attr.radioButtonStyle); |
| 57 | } |
| 58 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 59 | public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 60 | this(context, attrs, defStyleAttr, 0); |
| 61 | } |
| 62 | |
| 63 | public RadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 64 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * {@inheritDoc} |
| 69 | * <p> |
| 70 | * If the radio button is already checked, this method will not toggle the radio button. |
| 71 | */ |
| 72 | @Override |
| 73 | public void toggle() { |
| 74 | // we override to prevent toggle when the radio is already |
| 75 | // checked (as opposed to check boxes widgets) |
| 76 | if (!isChecked()) { |
| 77 | super.toggle(); |
| 78 | } |
| 79 | } |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 82 | public CharSequence getAccessibilityClassName() { |
| 83 | return RadioButton.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 84 | } |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 85 | |
| 86 | @Override |
| 87 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 88 | super.onInitializeAccessibilityNodeInfo(info); |
| 89 | if (getParent() instanceof RadioGroup) { |
| 90 | RadioGroup radioGroup = (RadioGroup) getParent(); |
| 91 | if (radioGroup.getOrientation() == LinearLayout.HORIZONTAL) { |
| 92 | info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain(0, 1, |
| 93 | radioGroup.getIndexWithinVisibleButtons(this), 1, false, isChecked())); |
| 94 | } else { |
| 95 | info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain( |
| 96 | radioGroup.getIndexWithinVisibleButtons(this), 1, 0, 1, |
| 97 | false, isChecked())); |
| 98 | } |
| 99 | } |
| 100 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |