当前位置:   article > 正文

RadioGroup+RadioButton嵌套其他布局实现多行单选布局、自定义RadioButton选中和非选中样式、文字颜色_radiogroup 里可以放其他控件吗

radiogroup 里可以放其他控件吗




上面两个图的需求都是要实现在多行中实现单选,本次以第二张图片的实现为例:
考虑到使用RadioGroup和RadioButton来做到单选的效果,为了实现上面的布局,在RadioGroup中嵌套了LinearLayout,然后在LinearLayout中分别加入相应的RadioButton。
运行起来看看效果,发现这样写就让RadioButton本身的“选择互斥性”丧失了。也就无法满足我们的需求。
所以,我们自定义RadioGroup(此RadioGroup来源在文末,由于使用中有些小问题,这里修改了一下), 解决RadioGroup嵌套其他布局后 RadioButton不再互斥的问题

  1. public class MyRadioGroup extends LinearLayout {
  2. // holds the checked id; the selection is empty by default
  3. private int mCheckedId = -1;
  4. // tracks children radio buttons checked state
  5. private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
  6. // when true, mOnCheckedChangeListener discards events
  7. private boolean mProtectFromCheckedChange = false;
  8. private OnCheckedChangeListener mOnCheckedChangeListener;
  9. private PassThroughHierarchyChangeListener mPassThroughListener;
  10. /**
  11. * {@inheritDoc}
  12. */
  13. public MyRadioGroup(Context context) {
  14. super(context);
  15. setOrientation(VERTICAL);
  16. init();
  17. }
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public MyRadioGroup(Context context, AttributeSet attrs) {
  22. super(context, attrs);
  23. mCheckedId = View.NO_ID;
  24. final int index = VERTICAL;
  25. setOrientation(index);
  26. init();
  27. }
  28. private void init() {
  29. mChildOnCheckedChangeListener = new CheckedStateTracker();
  30. mPassThroughListener = new PassThroughHierarchyChangeListener();
  31. super.setOnHierarchyChangeListener(mPassThroughListener);
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. @Override
  37. public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
  38. // the user listener is delegated to our pass-through listener
  39. mPassThroughListener.mOnHierarchyChangeListener = listener;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. @Override
  45. protected void onFinishInflate() {
  46. super.onFinishInflate();
  47. // checks the appropriate radio button as requested in the XML file
  48. if (mCheckedId != -1) {
  49. mProtectFromCheckedChange = true;
  50. setCheckedStateForView(mCheckedId, true);
  51. mProtectFromCheckedChange = false;
  52. setCheckedId(mCheckedId);
  53. }
  54. }
  55. @Override
  56. public void addView(final View child, int index, ViewGroup.LayoutParams params) {
  57. if (child instanceof RadioButton) {
  58. child.setOnTouchListener(new OnTouchListener() {
  59. @Override
  60. public boolean onTouch(View v, MotionEvent event) {
  61. if (event.getAction() == MotionEvent.ACTION_DOWN && !((RadioButton) child).isChecked()) {
  62. ((RadioButton) child).setChecked(true);
  63. checkRadioButton((RadioButton) child);
  64. if (mOnCheckedChangeListener != null) {
  65. mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, child.getId());
  66. }
  67. }
  68. return true;
  69. }
  70. });
  71. } else if (child instanceof LinearLayout) {
  72. int childCount = ((LinearLayout) child).getChildCount();
  73. for (int i = 0; i < childCount; i++) {
  74. View view = ((LinearLayout) child).getChildAt(i);
  75. if (view instanceof RadioButton) {
  76. final RadioButton button = (RadioButton) view;
  77. button.setOnTouchListener(new OnTouchListener() {
  78. @Override
  79. public boolean onTouch(View v, MotionEvent event) {
  80. if (event.getAction() == MotionEvent.ACTION_DOWN && !button.isChecked()) {
  81. button.setChecked(true);
  82. checkRadioButton(button);
  83. if (mOnCheckedChangeListener != null) {
  84. mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId());
  85. }
  86. }
  87. return true;
  88. }
  89. });
  90. }
  91. }
  92. }
  93. super.addView(child, index, params);
  94. }
  95. private void checkRadioButton(RadioButton radioButton) {
  96. View child;
  97. int radioCount = getChildCount();
  98. for (int i = 0; i < radioCount; i++) {
  99. child = getChildAt(i);
  100. if (child instanceof RadioButton) {
  101. if (child == radioButton) {
  102. // do nothing
  103. } else {
  104. ((RadioButton) child).setChecked(false);
  105. }
  106. } else if (child instanceof LinearLayout) {
  107. int childCount = ((LinearLayout) child).getChildCount();
  108. for (int j = 0; j < childCount; j++) {
  109. View view = ((LinearLayout) child).getChildAt(j);
  110. if (view instanceof RadioButton) {
  111. final RadioButton button = (RadioButton) view;
  112. if (button == radioButton) {
  113. // do nothing
  114. } else {
  115. ((RadioButton) button).setChecked(false);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * <p>Sets the selection to the radio button whose identifier is passed in
  124. * parameter. Using -1 as the selection identifier clears the selection;
  125. * such an operation is equivalent to invoking {@link #clearCheck()}.</p>
  126. *
  127. * @param id the unique id of the radio button to select in this group
  128. * @see #getCheckedRadioButtonId()
  129. * @see #clearCheck()
  130. */
  131. public void check(int id) {
  132. // don't even bother
  133. if (id != -1 && (id == mCheckedId)) {
  134. return;
  135. }
  136. if (mCheckedId != -1) {
  137. setCheckedStateForView(mCheckedId, false);
  138. }
  139. if (id != -1) {
  140. setCheckedStateForView(id, true);
  141. }
  142. setCheckedId(id);
  143. }
  144. private void setCheckedId(int id) {
  145. mCheckedId = id;
  146. }
  147. private void setCheckedStateForView(int viewId, boolean checked) {
  148. View checkedView = findViewById(viewId);
  149. if (checkedView != null && checkedView instanceof RadioButton) {
  150. ((RadioButton) checkedView).setChecked(checked);
  151. }
  152. }
  153. /**
  154. * <p>Returns the identifier of the selected radio button in this group.
  155. * Upon empty selection, the returned value is -1.</p>
  156. *
  157. * @return the unique id of the selected radio button in this group
  158. * @attr ref android.R.styleable#RadioGroup_checkedButton
  159. * @see #check(int)
  160. * @see #clearCheck()
  161. */
  162. public int getCheckedRadioButtonId() {
  163. return mCheckedId;
  164. }
  165. /**
  166. * <p>Clears the selection. When the selection is cleared, no radio button
  167. * in this group is selected and {@link #getCheckedRadioButtonId()} returns
  168. * null.</p>
  169. *
  170. * @see #check(int)
  171. * @see #getCheckedRadioButtonId()
  172. */
  173. public void clearCheck() {
  174. check(-1);
  175. }
  176. /**
  177. * <p>Register a callback to be invoked when the checked radio button
  178. * changes in this group.</p>
  179. *
  180. * @param listener the callback to call on checked state change
  181. */
  182. public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
  183. mOnCheckedChangeListener = listener;
  184. }
  185. /**
  186. * {@inheritDoc}
  187. */
  188. @Override
  189. public LayoutParams generateLayoutParams(AttributeSet attrs) {
  190. return new MyRadioGroup.LayoutParams(getContext(), attrs);
  191. }
  192. /**
  193. * {@inheritDoc}
  194. */
  195. @Override
  196. protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
  197. return p instanceof RadioGroup.LayoutParams;
  198. }
  199. @Override
  200. protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
  201. return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  202. }
  203. @Override
  204. public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  205. super.onInitializeAccessibilityEvent(event);
  206. event.setClassName(RadioGroup.class.getName());
  207. }
  208. @Override
  209. public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  210. super.onInitializeAccessibilityNodeInfo(info);
  211. info.setClassName(RadioGroup.class.getName());
  212. }
  213. /**
  214. * <p>This set of layout parameters defaults the width and the height of
  215. * the children to {@link #WRAP_CONTENT} when they are not specified in the
  216. * XML file. Otherwise, this class ussed the value read from the XML file.</p>
  217. * <p>
  218. * <p>See
  219. * for a list of all child view attributes that this class supports.</p>
  220. */
  221. public static class LayoutParams extends LinearLayout.LayoutParams {
  222. /**
  223. * {@inheritDoc}
  224. */
  225. public LayoutParams(Context c, AttributeSet attrs) {
  226. super(c, attrs);
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. public LayoutParams(int w, int h) {
  232. super(w, h);
  233. }
  234. /**
  235. * {@inheritDoc}
  236. */
  237. public LayoutParams(int w, int h, float initWeight) {
  238. super(w, h, initWeight);
  239. }
  240. /**
  241. * {@inheritDoc}
  242. */
  243. public LayoutParams(ViewGroup.LayoutParams p) {
  244. super(p);
  245. }
  246. /**
  247. * {@inheritDoc}
  248. */
  249. public LayoutParams(MarginLayoutParams source) {
  250. super(source);
  251. }
  252. /**
  253. * <p>Fixes the child's width to
  254. * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and the child's
  255. * height to {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}
  256. * when not specified in the XML file.</p>
  257. *
  258. * @param a the styled attributes set
  259. * @param widthAttr the width attribute to fetch
  260. * @param heightAttr the height attribute to fetch
  261. */
  262. @Override
  263. protected void setBaseAttributes(TypedArray a,
  264. int widthAttr, int heightAttr) {
  265. if (a.hasValue(widthAttr)) {
  266. width = a.getLayoutDimension(widthAttr, "layout_width");
  267. } else {
  268. width = WRAP_CONTENT;
  269. }
  270. if (a.hasValue(heightAttr)) {
  271. height = a.getLayoutDimension(heightAttr, "layout_height");
  272. } else {
  273. height = WRAP_CONTENT;
  274. }
  275. }
  276. }
  277. /**
  278. * <p>Interface definition for a callback to be invoked when the checked
  279. * radio button changed in this group.</p>
  280. */
  281. public interface OnCheckedChangeListener {
  282. /**
  283. * <p>Called when the checked radio button has changed. When the
  284. * selection is cleared, checkedId is -1.</p>
  285. *
  286. * @param group the group in which the checked radio button has changed
  287. * @param checkedId the unique identifier of the newly checked radio button
  288. */
  289. public void onCheckedChanged(MyRadioGroup group, int checkedId);
  290. }
  291. private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
  292. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  293. // prevents from infinite recursion
  294. if (mProtectFromCheckedChange) {
  295. return;
  296. }
  297. mProtectFromCheckedChange = true;
  298. if (mCheckedId != -1) {
  299. setCheckedStateForView(mCheckedId, false);
  300. }
  301. mProtectFromCheckedChange = false;
  302. int id = buttonView.getId();
  303. setCheckedId(id);
  304. }
  305. }
  306. /**
  307. * <p>A pass-through listener acts upon the events and dispatches them
  308. * to another listener. This allows the table layout to set its own internal
  309. * hierarchy change listener without preventing the user to setup his.</p>
  310. */
  311. private class PassThroughHierarchyChangeListener implements
  312. ViewGroup.OnHierarchyChangeListener {
  313. private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
  314. /**
  315. * {@inheritDoc}
  316. */
  317. public void onChildViewAdded(View parent, View child) {
  318. if (parent == MyRadioGroup.this && child instanceof RadioButton) {
  319. int id = child.getId();
  320. // generates an id if it's missing
  321. if (id == View.NO_ID) {
  322. id = child.hashCode();
  323. child.setId(id);
  324. }
  325. ((RadioButton) child).setOnCheckedChangeListener(
  326. mChildOnCheckedChangeListener);
  327. }
  328. if (mOnHierarchyChangeListener != null) {
  329. mOnHierarchyChangeListener.onChildViewAdded(parent, child);
  330. }
  331. }
  332. /**
  333. * {@inheritDoc}
  334. */
  335. public void onChildViewRemoved(View parent, View child) {
  336. if (parent == MyRadioGroup.this && child instanceof RadioButton) {
  337. ((RadioButton) child).setOnCheckedChangeListener(null);
  338. }
  339. if (mOnHierarchyChangeListener != null) {
  340. mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
  341. }
  342. }
  343. }
  344. }



布局文件中使用:

  1. <com.roboocean.robosea.view.MyRadioGroup
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <LinearLayout style="@style/linearLayout_device_setting_nomal">
  5. <TextView
  6. style="@style/textview_fps"
  7. android:text="1440P" />
  8. <RadioButton
  9. android:id="@+id/mFPS_1440_30"
  10. style="@style/radiobutton_fps"
  11. android:layout_marginRight="@dimen/x60"
  12. android:text="@string/fps_30" />
  13. </LinearLayout>
  14. <View
  15. android:layout_width="match_parent"
  16. android:layout_height="@dimen/y1"
  17. android:background="@color/bottom_line" />
  18. <LinearLayout style="@style/linearLayout_device_setting_nomal">
  19. <TextView
  20. style="@style/textview_fps"
  21. android:text="1080P" />
  22. <RadioButton
  23. android:checked="true"
  24. android:id="@+id/mFPS_1080_30"
  25. style="@style/radiobutton_fps"
  26. android:layout_marginRight="@dimen/x40"
  27. android:text="@string/fps_30" />
  28. <RadioButton
  29. android:id="@+id/mFPS_1080_60"
  30. style="@style/radiobutton_fps"
  31. android:layout_marginRight="@dimen/x60"
  32. android:text="@string/fps_60" />
  33. </LinearLayout>
  34. <View
  35. android:layout_width="match_parent"
  36. android:layout_height="@dimen/y1"
  37. android:background="@color/bottom_line" />
  38. <LinearLayout style="@style/linearLayout_device_setting_nomal">
  39. <TextView
  40. style="@style/textview_fps"
  41. android:text="720P" />
  42. <RadioButton
  43. android:id="@+id/mFPS_720_30"
  44. style="@style/radiobutton_fps"
  45. android:layout_marginRight="@dimen/x40"
  46. android:text="@string/fps_30" />
  47. <RadioButton
  48. android:id="@+id/mFPS_720_60"
  49. style="@style/radiobutton_fps"
  50. android:layout_marginRight="@dimen/x60"
  51. android:text="@string/fps_60" />
  52. </LinearLayout>
  53. </com.roboocean.robosea.view.MyRadioGroup>

相关样式

  1. <style name="linearLayout_device_setting_nomal">
  2. <item name="android:layout_width">match_parent</item>
  3. <item name="android:layout_height">@dimen/y90</item>
  4. <item name="android:gravity">center_vertical</item>
  5. <item name="android:background">@color/white</item>
  6. </style>


  1. <style name="textview_fps">
  2. <item name="android:layout_width">wrap_content</item>
  3. <item name="android:layout_height">wrap_content</item>
  4. <item name="android:layout_marginLeft">@dimen/x50</item>
  5. <item name="android:layout_weight">1</item>
  6. <item name="android:textColor">@color/empty_tip_text</item>
  7. <item name="android:textSize">@dimen/x29</item>
  8. </style>


  1. <style name="radiobutton_fps">
  2. <item name="android:button">@null</item>
  3. <item name="android:background">@drawable/selector_fps</item>
  4. <item name="android:layout_width">@dimen/x118</item>
  5. <item name="android:layout_height">@dimen/y58</item>
  6. <item name="android:textColor">@drawable/selector_fps_text</item>
  7. <item name="android:textSize">@dimen/x25</item>
  8. <item name="android:gravity">center</item>
  9. </style>


自定义RadioButton的Selector

selector_fps.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/shape_fps_normal" android:state_checked="false" ></item>
  4. <item android:drawable="@drawable/shape_fps_checked" android:state_checked="true"></item>
  5. </selector>


两个状态的shape:
shape_fps_normal.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <corners android:radius="10px" />
  4. <solid android:color="@color/white" />
  5. <stroke
  6. android:width="1dp"
  7. android:color="#b7b7b7" />
  8. </shape>

shape_fps_checked.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <corners android:radius="10px" />
  4. <solid android:color="@color/pressed_text" />
  5. <stroke
  6. android:width="1dp"
  7. android:color="@color/pressed_text" />
  8. </shape>

自定义RadioButton字体颜色的Selector:

selector_fps_text.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="true" android:color="@color/white"/>
  4. <item android:state_checked="false" android:color="@color/empty_tip_text"/>
  5. </selector>



上文中自定义RadioGroup源自:http://blog.csdn.net/yuanzihui/article/details/50462496,感谢分享

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号