自定义单项选择器和多项选择器 发表于 2016-09-06 | 单项选择器单项选择器采用继承RadioGroup,代码如下: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990public class DayChooseRadioGroup extends RadioGroup { private Context mContext; // holds the checked id; the selection is empty by default private int mCheckedId = -1; private int mRadioWith; private int mRadioHeight; // 存放当前的radioButton private String mData[] = {"2", "3", "4", "5", "6", "7"}; public DayChooseRadioGroup(Context context) { this(context, null); } public DayChooseRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { setOrientation(HORIZONTAL); mContext = context; mRadioWith = (DrawUtil.sWidthPixels - DrawUtil.dip2px(30) - 6 * DrawUtil.dip2px(13)) / 7; mRadioHeight = mRadioWith; int margin = (DrawUtil.sWidthPixels - mRadioWith * mData.length - DrawUtil.dip2px(30)) / (mData.length - 1); LayoutParams params = new LayoutParams(mRadioWith, mRadioHeight); params.rightMargin = margin; params.gravity = Gravity.CENTER; for (int i = 0; i < mData.length; i++) { RadioButton radioButton = new RadioButton(mContext); radioButton.setLayoutParams(params); radioButton.setButtonDrawable(android.R.color.transparent); radioButton.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); radioButton.setText(mData[i]); radioButton.setGravity(Gravity.CENTER); radioButton.setPadding(0, 0, 0, 0); radioButton.setTextSize(21); radioButton.setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector)); addView(radioButton); } } /** * 初始化设置默认选中 */ public void setCheckView(int checkedId) { for (int i = 0; i < getChildCount(); i++) { if (i == (checkedId - 2)) { this.check(getChildAt(i).getId()); } } } /** * 不可点击 */ public void disableRadioGroup() { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).setEnabled(false); if (getChildAt(i).getId() == getCheckedRadioButtonId()) { getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_background_selector)); } else { ((RadioButton) getChildAt(i)).setTextColor(0xffc4c6cb); getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_circle_selector)); } } } /** * 使能可点击 */ public void enableRadioGroup() { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).setEnabled(true); if (getChildAt(i).getId() == getCheckedRadioButtonId()) { getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); } else { ((RadioButton) getChildAt(i)).setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector)); getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); } } }} 多项选择器多项选择器采用复选控件CheckBox, 继承LinearLayout,代码如下: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142public class DayChooseCheckBox extends LinearLayout { private Context mContext; private int mRadioWith; private int mRadioHeight; private String mData[] = {"一", "二", "三", "四", "五", "六", "日"}; List<Integer> mChooseDays = new ArrayList<>(); private CheckedStateTracker mChildOnCheckedChangeListener; private OnCheckedChangeListener mOnCheckedChangeListener; public DayChooseCheckBox(Context context) { this(context, null); } public DayChooseCheckBox(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DayChooseCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { setOrientation(HORIZONTAL); mContext = context; mChildOnCheckedChangeListener = new CheckedStateTracker(); mRadioWith = (DrawUtil.sWidthPixels - DrawUtil.dip2px(30) - (mData.length - 1) * DrawUtil.dip2px(13)) / mData.length; mRadioHeight = mRadioWith; LayoutParams params = new LayoutParams(mRadioWith, mRadioHeight); params.rightMargin = DrawUtil.dip2px(13); params.gravity = Gravity.CENTER; for (int i = 0; i < mData.length; i++) { CheckBox checkBox = new CheckBox(mContext); checkBox.setLayoutParams(params); checkBox.setButtonDrawable(android.R.color.transparent); checkBox.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); checkBox.setText(mData[i]); checkBox.setTag(i); checkBox.setOnCheckedChangeListener(mChildOnCheckedChangeListener); checkBox.setGravity(Gravity.CENTER); checkBox.setPadding(0, 0, 0, 0); checkBox.setTextSize(18); checkBox.setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector)); addView(checkBox); } } /** * 初始化设置默认选中 */ public void setCheckViews(List<Integer> checkedIds) { for (int i = 0; i < getChildCount(); i++) { for (int j = 0; j < checkedIds.size(); j++) { if (i == (checkedIds.get(j) - 1)) { ((CheckBox) getChildAt(i)).setChecked(true); } } } } /** * 不可点击 */ public void disableRadioGroup() { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).setEnabled(false); if (((CheckBox) getChildAt(i)).isChecked()) { getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_background_selector)); } else { ((CheckBox) getChildAt(i)).setTextColor(0xffc4c6cb); getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_circle_selector)); } } } /** * 使能可点击 */ public void enableRadioGroup() { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).setEnabled(true); if (((CheckBox) getChildAt(i)).isChecked()) { getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); } else { ((CheckBox) getChildAt(i)).setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector)); getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector)); } } } /** * 将结果回调到外部使用 */ public interface OnCheckedChangeListener { void onCheckedChanged(DayChooseCheckBox group, List<Integer> chooseDays); } private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int day = (int) buttonView.getTag() + 1; if (isChecked) { mChooseDays.add(day); } else { Iterator<Integer> iterator = mChooseDays.iterator(); while (iterator.hasNext()) { int b = iterator.next(); if (b == day) { iterator.remove(); } } } Log.i("zou", "<DayChooseCheckBox> mOnCheckedChangeListener day= " + day + " mChooseDays.size() = " + mChooseDays.size()); setCheckedId(mChooseDays); } } public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { mOnCheckedChangeListener = listener; } private void setCheckedId(List<Integer> chooseDays) { if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(this, chooseDays); } }}