Android RadioGroup 自定义布局, 支持多行多列

摘抄至:http://blog.csdn.net/zhoulv2000/article/details/17578727

经过反复试验,发现无论你怎么摆弄RadioGroup, 都无法实现多行多列的单选按钮组。
1. 如果不结合其他布局,例如LinearLayout, 则只能实现单行多个按钮组,或者单列多个按钮组。
2. 如果结合其他布局, 虽然可以实现多行多列的RadioButton布局,但是,如果不通过一些互斥算法,也无法实现按钮组的单选操作。


    所以,我对RadioGroup进行了改写.

Java代码 收藏代码
  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. * 2013.11.06 updated by xiexiaojian
  17. * Support that LinearLayouts or RadioButtons can be all RadioGroup's childrens,
  18. * we can display more rows and columns at the same time.
  19. */ 
  20.  
  21. package tv.pps.mobile.newipd.version3.custom.view; 
  22.  
  23. import android.content.Context; 
  24. import android.content.res.TypedArray; 
  25. import android.util.AttributeSet; 
  26. import android.view.MotionEvent; 
  27. import android.view.View; 
  28. import android.view.ViewGroup; 
  29. import android.view.accessibility.AccessibilityEvent; 
  30. import android.view.accessibility.AccessibilityNodeInfo; 
  31. import android.widget.CompoundButton; 
  32. import android.widget.LinearLayout; 
  33. import android.widget.RadioButton; 
  34.  
  35.  
  36. /**
  37. * <p>This class is used to create a multiple-exclusion scope for a set of radio
  38. * buttons. Checking one radio button that belongs to a radio group unchecks
  39. * any previously checked radio button within the same group.</p>
  40. *
  41. * <p>Intially, all of the radio buttons are unchecked. While it is not possible
  42. * to uncheck a particular radio button, the radio group can be cleared to
  43. * remove the checked state.</p>
  44. *
  45. * <p>The selection is identified by the unique id of the radio button as defined
  46. * in the XML layout file.</p>
  47. *
  48. * <p><strong>XML Attributes</strong></p>
  49. * <p>See {@link android.R.styleable#RadioGroup RadioGroup Attributes},
  50. * {@link android.R.styleable#LinearLayout LinearLayout Attributes},
  51. * {@link android.R.styleable#ViewGroup ViewGroup Attributes},
  52. * {@link android.R.styleable#View View Attributes}</p>
  53. * <p>Also see
  54. * {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}
  55. * for layout attributes.</p>
  56. *
  57. * @see RadioButton
  58. *
  59. */ 
  60. public class RadioGroup extends LinearLayout { 
  61.     // holds the checked id; the selection is empty by default 
  62.     private int mCheckedId = -1
  63.     // tracks children radio buttons checked state 
  64.     private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener; 
  65.     // when true, mOnCheckedChangeListener discards events 
  66.     private boolean mProtectFromCheckedChange = false
  67.     private OnCheckedChangeListener mOnCheckedChangeListener; 
  68.     private PassThroughHierarchyChangeListener mPassThroughListener; 
  69.  
  70.     /**
  71.      * {@inheritDoc}
  72.      */ 
  73.     public RadioGroup(Context context) { 
  74.         super(context); 
  75.         setOrientation(VERTICAL); 
  76.         init(); 
  77.     } 
  78.  
  79.     /**
  80.      * {@inheritDoc}
  81.      */ 
  82.     public RadioGroup(Context context, AttributeSet attrs) { 
  83.         super(context, attrs); 
  84.         mCheckedId = View.NO_ID; 
  85.  
  86.         final int index = VERTICAL; 
  87.         setOrientation(index); 
  88.  
  89.         init(); 
  90.     } 
  91.  
  92.     private void init() { 
  93.         mChildOnCheckedChangeListener = new CheckedStateTracker(); 
  94.         mPassThroughListener = new PassThroughHierarchyChangeListener(); 
  95.         super.setOnHierarchyChangeListener(mPassThroughListener); 
  96.     } 
  97.  
  98.     /**
  99.      * {@inheritDoc}
  100.      */ 
  101.     @Override 
  102.     public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { 
  103.         // the user listener is delegated to our pass-through listener 
  104.         mPassThroughListener.mOnHierarchyChangeListener = listener; 
  105.     } 
  106.  
  107.     /**
  108.      * {@inheritDoc}
  109.      */ 
  110.     @Override 
  111.     protected void onFinishInflate() { 
  112.         super.onFinishInflate(); 
  113.  
  114.         // checks the appropriate radio button as requested in the XML file 
  115.         if (mCheckedId != -1) { 
  116.             mProtectFromCheckedChange = true
  117.             setCheckedStateForView(mCheckedId, true); 
  118.             mProtectFromCheckedChange = false
  119.             setCheckedId(mCheckedId); 
  120.         } 
  121.     } 
  122.  
  123.     @Override 
  124.     public void addView(final View child, int index, ViewGroup.LayoutParams params) { 
  125.         if (child instanceof RadioButton) { 
  126.              
  127.             ((RadioButton) child).setOnTouchListener(new OnTouchListener() { 
  128.                  
  129.                 @Override 
  130.                 public boolean onTouch(View v, MotionEvent event) { 
  131.                     ((RadioButton) child).setChecked(true); 
  132.                     checkRadioButton((RadioButton) child); 
  133.                     if(mOnCheckedChangeListener != null){ 
  134.                         mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, child.getId()); 
  135.                     } 
  136.                     return true
  137.                 } 
  138.             }); 
  139.              
  140.         } else if(child instanceof LinearLayout){ 
  141.             int childCount = ((LinearLayout) child).getChildCount(); 
  142.             for(int i = 0; i < childCount; i++){ 
  143.                 View view = ((LinearLayout) child).getChildAt(i); 
  144.                 if (view instanceof RadioButton) { 
  145.                     final RadioButton button = (RadioButton) view; 
  146.  
  147.                      
  148.                     ((RadioButton) button).setOnTouchListener(new OnTouchListener() { 
  149.                          
  150.                         @Override 
  151.                         public boolean onTouch(View v, MotionEvent event) { 
  152.                             ((RadioButton) button).setChecked(true); 
  153.                             checkRadioButton((RadioButton) button); 
  154.                             if(mOnCheckedChangeListener != null){ 
  155.                                 mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, button.getId()); 
  156.                             } 
  157.                             return true
  158.                         } 
  159.                     }); 
  160.                      
  161.                 } 
  162.             } 
  163.         } 
  164.  
  165.         super.addView(child, index, params); 
  166.     } 
  167.      
  168.     private void checkRadioButton(RadioButton radioButton){ 
  169.         View child; 
  170.         int radioCount = getChildCount(); 
  171.         for(int i = 0; i < radioCount; i++){ 
  172.             child = getChildAt(i); 
  173.             if (child instanceof RadioButton) { 
  174.                 if(child == radioButton){ 
  175.                     // do nothing 
  176.                 } else
  177.                     ((RadioButton) child).setChecked(false); 
  178.                 } 
  179.             } else if(child instanceof LinearLayout){ 
  180.                 int childCount = ((LinearLayout) child).getChildCount(); 
  181.                 for(int j = 0; j < childCount; j++){ 
  182.                     View view = ((LinearLayout) child).getChildAt(j); 
  183.                     if (view instanceof RadioButton) { 
  184.                         final RadioButton button = (RadioButton) view; 
  185.                         if(button == radioButton){ 
  186.                             // do nothing 
  187.                         } else
  188.                             ((RadioButton) button).setChecked(false); 
  189.                         } 
  190.                     } 
  191.                 } 
  192.             } 
  193.         } 
  194.     } 
  195.  
  196.     /**
  197.      * <p>Sets the selection to the radio button whose identifier is passed in
  198.      * parameter. Using -1 as the selection identifier clears the selection;
  199.      * such an operation is equivalent to invoking {@link #clearCheck()}.</p>
  200.      *
  201.      * @param id the unique id of the radio button to select in this group
  202.      *
  203.      * @see #getCheckedRadioButtonId()
  204.      * @see #clearCheck()
  205.      */ 
  206.     public void check(int id) { 
  207.         // don't even bother 
  208.         if (id != -1 && (id == mCheckedId)) { 
  209.             return
  210.         } 
  211.  
  212.         if (mCheckedId != -1) { 
  213.             setCheckedStateForView(mCheckedId, false); 
  214.         } 
  215.  
  216.         if (id != -1) { 
  217.             setCheckedStateForView(id, true); 
  218.         } 
  219.  
  220.         setCheckedId(id); 
  221.     } 
  222.  
  223.     private void setCheckedId(int id) { 
  224.         mCheckedId = id; 
  225.     } 
  226.  
  227.     private void setCheckedStateForView(int viewId, boolean checked) { 
  228.         View checkedView = findViewById(viewId); 
  229.         if (checkedView != null && checkedView instanceof RadioButton) { 
  230.             ((RadioButton) checkedView).setChecked(checked); 
  231.         } 
  232.     } 
  233.  
  234.     /**
  235.      * <p>Returns the identifier of the selected radio button in this group.
  236.      * Upon empty selection, the returned value is -1.</p>
  237.      *
  238.      * @return the unique id of the selected radio button in this group
  239.      *
  240.      * @see #check(int)
  241.      * @see #clearCheck()
  242.      *
  243.      * @attr ref android.R.styleable#RadioGroup_checkedButton
  244.      */ 
  245.     public int getCheckedRadioButtonId() { 
  246.         return mCheckedId; 
  247.     } 
  248.  
  249.     /**
  250.      * <p>Clears the selection. When the selection is cleared, no radio button
  251.      * in this group is selected and {@link #getCheckedRadioButtonId()} returns
  252.      * null.</p>
  253.      *
  254.      * @see #check(int)
  255.      * @see #getCheckedRadioButtonId()
  256.      */ 
  257.     public void clearCheck() { 
  258.         check(-1); 
  259.     } 
  260.  
  261.     /**
  262.      * <p>Register a callback to be invoked when the checked radio button
  263.      * changes in this group.</p>
  264.      *
  265.      * @param listener the callback to call on checked state change
  266.      */ 
  267.     public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { 
  268.         mOnCheckedChangeListener = listener; 
  269.     } 
  270.  
  271.     /**
  272.      * {@inheritDoc}
  273.      */ 
  274.     @Override 
  275.     public LayoutParams generateLayoutParams(AttributeSet attrs) { 
  276.         return new RadioGroup.LayoutParams(getContext(), attrs); 
  277.     } 
  278.  
  279.     /**
  280.      * {@inheritDoc}
  281.      */ 
  282.     @Override 
  283.     protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 
  284.         return p instanceof RadioGroup.LayoutParams; 
  285.     } 
  286.  
  287.     @Override 
  288.     protected LinearLayout.LayoutParams generateDefaultLayoutParams() { 
  289.         return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
  290.     } 
  291.  
  292.     @Override 
  293.     public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 
  294.         super.onInitializeAccessibilityEvent(event); 
  295.         event.setClassName(RadioGroup.class.getName()); 
  296.     } 
  297.  
  298.     @Override 
  299.     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 
  300.         super.onInitializeAccessibilityNodeInfo(info); 
  301.         info.setClassName(RadioGroup.class.getName()); 
  302.     } 
  303.  
  304.     /**
  305.      * <p>This set of layout parameters defaults the width and the height of
  306.      * the children to {@link #WRAP_CONTENT} when they are not specified in the
  307.      * XML file. Otherwise, this class ussed the value read from the XML file.</p>
  308.      *
  309.      * <p>See
  310.      * {@link android.R.styleable#LinearLayout_Layout LinearLayout Attributes}
  311.      * for a list of all child view attributes that this class supports.</p>
  312.      *
  313.      */ 
  314.     public static class LayoutParams extends LinearLayout.LayoutParams { 
  315.         /**
  316.          * {@inheritDoc}
  317.          */ 
  318.         public LayoutParams(Context c, AttributeSet attrs) { 
  319.             super(c, attrs); 
  320.         } 
  321.  
  322.         /**
  323.          * {@inheritDoc}
  324.          */ 
  325.         public LayoutParams(int w, int h) { 
  326.             super(w, h); 
  327.         } 
  328.  
  329.         /**
  330.          * {@inheritDoc}
  331.          */ 
  332.         public LayoutParams(int w, int h, float initWeight) { 
  333.             super(w, h, initWeight); 
  334.         } 
  335.  
  336.         /**
  337.          * {@inheritDoc}
  338.          */ 
  339.         public LayoutParams(ViewGroup.LayoutParams p) { 
  340.             super(p); 
  341.         } 
  342.  
  343.         /**
  344.          * {@inheritDoc}
  345.          */ 
  346.         public LayoutParams(MarginLayoutParams source) { 
  347.             super(source); 
  348.         } 
  349.  
  350.         /**
  351.          * <p>Fixes the child's width to
  352.          * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and the child's
  353.          * height to  {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}
  354.          * when not specified in the XML file.</p>
  355.          *
  356.          * @param a the styled attributes set
  357.          * @param widthAttr the width attribute to fetch
  358.          * @param heightAttr the height attribute to fetch
  359.          */ 
  360.         @Override 
  361.         protected void setBaseAttributes(TypedArray a, 
  362.                 int widthAttr, int heightAttr) { 
  363.  
  364.             if (a.hasValue(widthAttr)) { 
  365.                 width = a.getLayoutDimension(widthAttr, "layout_width"); 
  366.             } else
  367.                 width = WRAP_CONTENT; 
  368.             } 
  369.              
  370.             if (a.hasValue(heightAttr)) { 
  371.                 height = a.getLayoutDimension(heightAttr, "layout_height"); 
  372.             } else
  373.                 height = WRAP_CONTENT; 
  374.             } 
  375.         } 
  376.     } 
  377.  
  378.     /**
  379.      * <p>Interface definition for a callback to be invoked when the checked
  380.      * radio button changed in this group.</p>
  381.      */ 
  382.     public interface OnCheckedChangeListener { 
  383.         /**
  384.          * <p>Called when the checked radio button has changed. When the
  385.          * selection is cleared, checkedId is -1.</p>
  386.          *
  387.          * @param group the group in which the checked radio button has changed
  388.          * @param checkedId the unique identifier of the newly checked radio button
  389.          */ 
  390.         public void onCheckedChanged(RadioGroup group, int checkedId); 
  391.     } 
  392.  
  393.     private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener { 
  394.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
  395.             // prevents from infinite recursion 
  396.             if (mProtectFromCheckedChange) { 
  397.                 return
  398.             } 
  399.  
  400.             mProtectFromCheckedChange = true
  401.             if (mCheckedId != -1) { 
  402.                 setCheckedStateForView(mCheckedId, false); 
  403.             } 
  404.             mProtectFromCheckedChange = false
  405.  
  406.             int id = buttonView.getId(); 
  407.             setCheckedId(id); 
  408.         } 
  409.     } 
  410.  
  411.     /**
  412.      * <p>A pass-through listener acts upon the events and dispatches them
  413.      * to another listener. This allows the table layout to set its own internal
  414.      * hierarchy change listener without preventing the user to setup his.</p>
  415.      */ 
  416.     private class PassThroughHierarchyChangeListener implements 
  417.             ViewGroup.OnHierarchyChangeListener { 
  418.         private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener; 
  419.  
  420.         /**
  421.          * {@inheritDoc}
  422.          */ 
  423.         public void onChildViewAdded(View parent, View child) { 
  424.             if (parent == RadioGroup.this && child instanceof RadioButton) { 
  425.                 int id = child.getId(); 
  426.                 // generates an id if it's missing 
  427.                 if (id == View.NO_ID) { 
  428.                     id = child.hashCode(); 
  429.                     child.setId(id); 
  430.                 } 
  431.                 ((RadioButton) child).setOnCheckedChangeListener( 
  432.                         mChildOnCheckedChangeListener); 
  433.             } 
  434.  
  435.             if (mOnHierarchyChangeListener != null) { 
  436.                 mOnHierarchyChangeListener.onChildViewAdded(parent, child); 
  437.             } 
  438.         } 
  439.  
  440.         /**
  441.          * {@inheritDoc}
  442.          */ 
  443.         public void onChildViewRemoved(View parent, View child) { 
  444.             if (parent == RadioGroup.this && child instanceof RadioButton) { 
  445.                 ((RadioButton) child).setOnCheckedChangeListener(null); 
  446.             } 
  447.  
  448.             if (mOnHierarchyChangeListener != null) { 
  449.                 mOnHierarchyChangeListener.onChildViewRemoved(parent, child); 
  450.             } 
  451.         } 
  452.     } 


其中的这一段是关键改写代码:
Java代码 收藏代码
  1.    @Override 
  2. public void addView(final View child, int index, ViewGroup.LayoutParams params) { 
  3.     if (child instanceof RadioButton) { 
  4.          
  5.         ((RadioButton) child).setOnTouchListener(new OnTouchListener() { 
  6.  
  7. @Override 
  8. public boolean onTouch(View v, MotionEvent event) { 
  9.     ((RadioButton) child).setChecked(true); 
  10.     checkRadioButton((RadioButton) child); 
  11.     if(mOnCheckedChangeListener != null){ 
  12.         mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, child.getId()); 
  13.     } 
  14.     return true
  15. ); 
  16.          
  17.     } else if(child instanceof LinearLayout){ 
  18.         int childCount = ((LinearLayout) child).getChildCount(); 
  19.         for(int i = 0; i < childCount; i++){ 
  20.             View view = ((LinearLayout) child).getChildAt(i); 
  21.             if (view instanceof RadioButton) { 
  22.                 final RadioButton button = (RadioButton) view; 
  23.  
  24.                  
  25.                 ((RadioButton) button).setOnTouchListener(new OnTouchListener() { 
  26.                      
  27.                     @Override 
  28.                     public boolean onTouch(View v, MotionEvent event) { 
  29.                         ((RadioButton) button).setChecked(true); 
  30.                         checkRadioButton((RadioButton) button); 
  31.                         if(mOnCheckedChangeListener != null){ 
  32.                             mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, button.getId()); 
  33.                         } 
  34.                         return true
  35.                     } 
  36.                 }); 
  37.                  
  38.             } 
  39.         } 
  40.     } 
  41.  
  42.     super.addView(child, index, params); 
  43.  
  44. private void checkRadioButton(RadioButton radioButton){ 
  45.     View child; 
  46.     int radioCount = getChildCount(); 
  47.     for(int i = 0; i < radioCount; i++){ 
  48.         child = getChildAt(i); 
  49.         if (child instanceof RadioButton) { 
  50.             if(child == radioButton){ 
  51.                 // do nothing 
  52.             } else
  53.                 ((RadioButton) child).setChecked(false); 
  54.             } 
  55.         } else if(child instanceof LinearLayout){ 
  56.             int childCount = ((LinearLayout) child).getChildCount(); 
  57.             for(int j = 0; j < childCount; j++){ 
  58.                 View view = ((LinearLayout) child).getChildAt(j); 
  59.                 if (view instanceof RadioButton) { 
  60.                     final RadioButton button = (RadioButton) view; 
  61.                     if(button == radioButton){ 
  62.                         // do nothing 
  63.                     } else
  64.                         ((RadioButton) button).setChecked(false); 
  65.                     } 
  66.                 } 
  67.             } 
  68.         } 
  69.     } 


布局文件我就不贴出来了。

实现结果如下图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值