/** * <p>Returns the identifier of the selected radio button in this group. * Upon empty selection, the returned value is -1. * * Note: If this group is NOT exclusive this will return the first checked id, * there may be multiple checked ids! * * @return the unique id of the selected radio button in this group * * @see #check(int) */ @IdRes public int getCheckedId() { return getExclusiveCheckedId(); }
/** * {@inheritDoc} */ @Override protected void onFinishInflate() { super.onFinishInflate(); // checks the appropriate radio button as requested in the XML file int initialCheck = getExclusiveCheckedId(); if (initialCheck != View.NO_ID) { mProtectFromCheckedChange = true; setCheckedStateForView(initialCheck, true); mProtectFromCheckedChange = false; addCheckedId(initialCheck); } }
@Override public void addView(View child, int index, ViewGroup.LayoutParams params) { if (child instanceof CompoundButton) { final CompoundButton button = (CompoundButton) child; if (button.isChecked()) { mProtectFromCheckedChange = true; int currentCheck = getExclusiveCheckedId(); if (mExclusive && currentCheck != View.NO_ID) { setCheckedStateForView(currentCheck, false); } mProtectFromCheckedChange = false; addCheckedId(button.getId()); } } super.addView(child, index, params); }
private void checkExclusive(@IdRes int id) { int currentCheck = getExclusiveCheckedId(); // If no id, then clear if (id == NO_ID) { removeAllChecked(); } else if (id == currentCheck) { // If we've been sent an already checked item if(mAllowUnselected) { // If we don't allow unselected we simply do nothing in this method. setCheckedStateForView(id, false); removeCheckedId(id); } } else { // Unchecked item selected if (currentCheck != NO_ID) { // Uncheck existing selection setCheckedStateForView(currentCheck, false); } setCheckedStateForView(id, true); addCheckedId(id); } }