f14d0611b75b31adbd4d771fc87b0d28548a3c41.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package eVVM.apk.helper;
  2. import android.support.annotation.IdRes;
  3. import android.view.View;
  4. import android.widget.CompoundButton;
  5. import android.widget.RadioButton;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. /**
  9. * desc : 多个 CompoundButton 选中处理辅助类(用于代替 RadioGroup)
  10. */
  11. public final class RadioButtonGroupHelper implements CompoundButton.OnCheckedChangeListener {
  12. private List<RadioButton> mViewSet;//RadioButton集合
  13. private OnCheckedChangeListener mListener;//多个RadioButton监听对象
  14. public RadioButtonGroupHelper(RadioButton... groups) {
  15. mViewSet = new ArrayList<>(groups.length - 1);
  16. for (RadioButton view : groups) {
  17. // 如果这个RadioButton没有设置id的话
  18. if (view.getId() == View.NO_ID) {
  19. throw new IllegalArgumentException("The resource id must be set for the RadioButton");
  20. }
  21. view.setOnCheckedChangeListener(this);
  22. mViewSet.add(view);
  23. }
  24. }
  25. public RadioButtonGroupHelper(View rootView, @IdRes int... ids) {
  26. mViewSet = new ArrayList<>(ids.length - 1);
  27. for (int id : ids) {
  28. RadioButton view = rootView.findViewById(id);
  29. view.setOnCheckedChangeListener(this);
  30. mViewSet.add(view);
  31. }
  32. }
  33. private boolean mTag; // 监听标记,避免重复回调
  34. /**
  35. * {@link CompoundButton.OnCheckedChangeListener}
  36. */
  37. @Override
  38. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  39. if (isChecked && !mTag) {
  40. mTag = true;
  41. for (CompoundButton view : mViewSet) {
  42. if (view != buttonView && view.isChecked()) {
  43. // 这个 API 会触发监听事件
  44. view.setChecked(false);
  45. }
  46. }
  47. if (mListener != null) {
  48. mListener.onCheckedChanged((RadioButton) buttonView, buttonView.getId());
  49. }
  50. mTag = false;
  51. }
  52. }
  53. /**
  54. * 移除监听,避免内存泄露
  55. */
  56. public void removeViews() {
  57. if (mViewSet == null) return;
  58. for (CompoundButton view : mViewSet) {
  59. view.setOnCheckedChangeListener(null);
  60. }
  61. mViewSet.clear();
  62. mViewSet = null;
  63. }
  64. /**
  65. * 取消选中
  66. */
  67. public void clearCheck() {
  68. for (CompoundButton view : mViewSet) {
  69. if (view.isChecked()) {
  70. view.setChecked(false);
  71. }
  72. }
  73. }
  74. /**
  75. * 设置多个RadioButton的监听
  76. */
  77. public void setOnCheckedChangeListener(OnCheckedChangeListener l) {
  78. mListener = l;
  79. }
  80. /**
  81. * 多个CompoundButton选中监听
  82. */
  83. public interface OnCheckedChangeListener {
  84. /**
  85. * 被选中的CompoundButton对象
  86. *
  87. * @param radioButton 选中的RadioButton
  88. * @param checkedId 选中的资源id
  89. */
  90. void onCheckedChanged(RadioButton radioButton, @IdRes int checkedId);
  91. }
  92. }