RadioGroup的简单封装
2015-12-31 11:33
351 查看
RadioGroup的简单封装 如代码:
/** * @outher 孙磊磊 * create at 2015/12/29 10:44 * description 单行多列选择器 */ public class SelectRadioGroup extends RadioGroup implements CompoundButton.OnCheckedChangeListener { private ChecdChangeListener mListener; private int mSelectPosition; private int mItenWidth; public SelectRadioGroup(Context context) { this(context, null); } public SelectRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); mItenWidth = (wm.getDefaultDisplay().getWidth() - 2 * DensityUtils.dp2px(getContext(), 5)) / 3; setGravity(Gravity.CENTER_VERTICAL); setOrientation(LinearLayout.HORIZONTAL); setPadding(getPx(5), getPx(5), getPx(5), getPx(5)); setItem("item1", "item2", "item3"); } /** * @outher 孙磊磊 * create at 2015/12/29 14:49 * description 提供数据源,设置item */ public void setItem(String... strings) { removeAllViews(); for (int i = 0; i < strings.length; i++) { addView(getItem(strings[i])); } setSelectItem(mSelectPosition); } /** * @outher 孙磊磊 * create at 2015/12/29 14:48 * description 初始化item,设置每个item宽度为屏幕三分之一左右 */ private RadioButton getItem(String str) { RadioButton radioButton = new RadioButton(getContext()); LayoutParams lp = new RadioGroup.LayoutParams(mItenWidth, LayoutParams.MATCH_PARENT); radioButton.setLayoutParams(lp); radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT)); radioButton.setGravity(Gravity.CENTER); radioButton.setTextSize(16); radioButton.setPadding(getPx(5), getPx(5), getPx(5), getPx(5)); radioButton.setTextColor(getContext().getResources().getColor(R.color.white)); radioButton.setOnCheckedChangeListener(this); radioButton.setText(str); return radioButton; } private int getPx(int dp) { return DensityUtils.dp2px(getContext(), dp); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { for (int i = 0; i < getChildCount(); i++) { RadioButton rb = (RadioButton) getChildAt(i); if (buttonView == rb) { mSelectPosition = i; if (mListener != null) { mListener.seletItem(i); } rb.setTextColor(getContext().getResources().getColor(R.color.white)); rb.setBackgroundResource(R.drawable.round_orange); } else { rb.setTextColor(getContext().getResources().getColor(R.color.grey_color)); rb.setBackgroundColor(getResources().getColor(R.color.bg_def)); } } } } public interface ChecdChangeListener { public void seletItem(int position); } public void setListener(ChecdChangeListener listener) { this.mListener = listener; } /** * @outher 孙磊磊 * create at 2015/12/29 14:47 * description 设置指定的item */ public void setSelectItem(int position) { if (position >= 0 && position < getChildCount()) { RadioButton rb = (RadioButton) getChildAt(position); // rb.setSelected(true); rb.performClick(); } } /** * @outher 孙磊磊 * create at 2015/12/29 14:47 * description 返回当前选定的item下标 */ public int getSelectIndex() { return mSelectPosition; } }使用时直接调用方法
public void setItem(String... strings);
不限定item个数,不指定时默认为三个,可嵌套在HorizontalScrollView内部,用于多选项滑动
效果图如下:
相关文章推荐
- 缓存jar包来提升spark on Yarn job的提交速度
- 华为測试 超长整数相加
- 编程英语大全
- PECL 和 PEAR 的区别
- PHP - Cookie
- 如何写一个计算器?
- 浅谈Android中的序列化
- 一幅图看懂5种动态磁盘类型
- arc engineKML转Layer
- 欢迎使用CSDN-markdown编辑器
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- javaBean为什么要实现 序列化 Serializable接口?
- Xcode中文件名旁边的小标记的作用
- Android屏幕适配
- URL中带加号的处理
- Eclipse RCP - 第一个RCP程序
- Session 与 jsessionid
- webench源码研究
- SQL server链接服务器的配置
- smile