自定义RadioGroup,实现单选功能

文章目录

    • 1、效果图:
    • 2、自定义MyRadioGroup
    • 3、布局文件
    • 4、具体使用

扩展RadioGroup,实现RadioButton换行可单选功能;

1、效果图:

自定义RadioGroup,实现单选功能_第1张图片

2、自定义MyRadioGroup

/**
 * @Dec :解决RadioGroup中两个RadioButton选中以及RadioButton换行布局的RadioGroup
 * 

* https://blog.csdn.net/yuanzihui/article/details/50462496 * @Author : Caowj * @Date : 2018/11/20 16:48 */ public class MyRadioGroup extends LinearLayout { // holds the checked id; the selection is empty by default private int mCheckedId = -1; // tracks children radio buttons checked state private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener; // when true, mOnCheckedChangeListener discards events private boolean mProtectFromCheckedChange = false; private OnCheckedChangeListener mOnCheckedChangeListener; private PassThroughHierarchyChangeListener mPassThroughListener; public MyRadioGroup(Context context) { super(context); setOrientation(VERTICAL); init(); } public MyRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); mCheckedId = View.NO_ID; final int index = VERTICAL; setOrientation(index); init(); } private void init() { mChildOnCheckedChangeListener = new CheckedStateTracker(); mPassThroughListener = new PassThroughHierarchyChangeListener(); super.setOnHierarchyChangeListener(mPassThroughListener); } @Override public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { // the user listener is delegated to our pass-through listener mPassThroughListener.mOnHierarchyChangeListener = listener; } @Override protected void onFinishInflate() { super.onFinishInflate(); // checks the appropriate radio button as requested in the XML file if (mCheckedId != -1) { mProtectFromCheckedChange = true; setCheckedStateForView(mCheckedId, true); mProtectFromCheckedChange = false; setCheckedId(mCheckedId); } } @Override public void addView(final View child, int index, ViewGroup.LayoutParams params) { if (child instanceof RadioButton) { ((RadioButton) child).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((RadioButton) child).setChecked(true); checkRadioButton((RadioButton) child); if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, child.getId()); } return true; } }); } else if (child instanceof LinearLayout) { int childCount = ((LinearLayout) child).getChildCount(); for (int i = 0; i < childCount; i++) { View view = ((LinearLayout) child).getChildAt(i); if (view instanceof RadioButton) { final RadioButton button = (RadioButton) view; ((RadioButton) button).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // LegoLog.d("onCheckedChanged..."); ((RadioButton) button).setChecked(true); checkRadioButton((RadioButton) button); if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId()); } return true; } }); } } } super.addView(child, index, params); } private void checkRadioButton(RadioButton radioButton) { View child; int radioCount = getChildCount(); for (int i = 0; i < radioCount; i++) { child = getChildAt(i); if (child instanceof RadioButton) { if (child == radioButton) { // do nothing } else { ((RadioButton) child).setChecked(false); } } else if (child instanceof LinearLayout) { int childCount = ((LinearLayout) child).getChildCount(); for (int j = 0; j < childCount; j++) { View view = ((LinearLayout) child).getChildAt(j); if (view instanceof RadioButton) { final RadioButton button = (RadioButton) view; if (button == radioButton) { // do nothing } else { ((RadioButton) button).setChecked(false); } } } } } } public void check(int id) { // don't even bother if (id != -1 && (id == mCheckedId)) { return; } if (mCheckedId != -1) { setCheckedStateForView(mCheckedId, false); } if (id != -1) { setCheckedStateForView(id, true); } setCheckedId(id); } private void setCheckedId(int id) { mCheckedId = id; } private void setCheckedStateForView(int viewId, boolean checked) { View checkedView = findViewById(viewId); if (checkedView != null && checkedView instanceof RadioButton) { ((RadioButton) checkedView).setChecked(checked); } } public int getCheckedRadioButtonId() { return mCheckedId; } public void clearCheck() { check(-1); } public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { mOnCheckedChangeListener = listener; } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MyRadioGroup.LayoutParams(getContext(), attrs); } @Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof MyRadioGroup.LayoutParams; } @Override protected LinearLayout.LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(MyRadioGroup.class.getName()); } @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(MyRadioGroup.class.getName()); } public interface OnCheckedChangeListener { public void onCheckedChanged(MyRadioGroup group, int checkedId); } public static class LayoutParams extends LinearLayout.LayoutParams { public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); } public LayoutParams(int w, int h) { super(w, h); } public LayoutParams(int w, int h, float initWeight) { super(w, h, initWeight); } public LayoutParams(ViewGroup.LayoutParams p) { super(p); } public LayoutParams(MarginLayoutParams source) { super(source); } @Override protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { if (a.hasValue(widthAttr)) { width = a.getLayoutDimension(widthAttr, "layout_width"); } else { width = WRAP_CONTENT; } if (a.hasValue(heightAttr)) { height = a.getLayoutDimension(heightAttr, "layout_height"); } else { height = WRAP_CONTENT; } } } private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // prevents from infinite recursion if (mProtectFromCheckedChange) { return; } mProtectFromCheckedChange = true; if (mCheckedId != -1) { setCheckedStateForView(mCheckedId, false); } mProtectFromCheckedChange = false; int id = buttonView.getId(); setCheckedId(id); } } private class PassThroughHierarchyChangeListener implements OnHierarchyChangeListener { private OnHierarchyChangeListener mOnHierarchyChangeListener; public void onChildViewAdded(View parent, View child) { if (parent == MyRadioGroup.this && child instanceof RadioButton) { int id = child.getId(); // generates an id if it's missing if (id == View.NO_ID) { id = child.hashCode(); child.setId(id); } ((RadioButton) child).setOnCheckedChangeListener( mChildOnCheckedChangeListener); } if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewAdded(parent, child); } } public void onChildViewRemoved(View parent, View child) { if (parent == MyRadioGroup.this && child instanceof RadioButton) { ((RadioButton) child).setOnCheckedChangeListener(null); } if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewRemoved(parent, child); } } } }

3、布局文件

1、xml文件



     

         

         

         
     

     

         

         

         
     
 

2、自定义的RadioButton样式:card_num_bg_blue.xml



    
    

同理定义其他样式。

4、具体使用

1、设置默认选中:
 //默认选中蓝色车牌
 rg_color_option.check(R.id.rb_item_blue)

2、监听选择的结果
rg_color_option.setOnCheckedChangeListener { group, checkedId ->
    val radioButton: RadioButton? = findViewById(checkedId)
    val tag: String = radioButton?.tag.toString()
    nViewModel.plateColor = tag
    // TODO 弊端:会回调多次
    LegoLog.d("tag->$tag")
}

你可能感兴趣的:(代码块,view)