Android 自定义View实现任意布局的RadioGroup效果
2019-01-15 12:03
1006 查看
前言
RadioGroup是继承LinearLayout,只支持横向或者竖向两种布局。所以在某些情况,比如多行多列布局,RadioGroup就并不适用 。
本篇文章通过继承RelativeLayout实现自定义RadioGroup,实现RadioButton的任意布局。效果图如下:
代码(RelativeRadioGroup)
/** * Author : BlackHao * Time : 2018/10/26 10:46 * Description : 自定义 RadioGroup */ public class RelativeRadioGroup extends RelativeLayout implements CompoundButton.OnCheckedChangeListener { private int checkId = -1; private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener; public RelativeRadioGroup(Context context) { super(context); } public RelativeRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); } public RelativeRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); //添加监听 for (int i = 0; i < getChildCount(); i++) { View v = getChildAt(i); if (v instanceof RadioButton && !(v instanceof CompoundButton.OnCheckedChangeListener)) { ((RadioButton) v).setOnCheckedChangeListener(this); } } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { setCheck(buttonView.getId(), isChecked); } public void check(@IdRes int checkId) { if (checkId == -1 || this.checkId == checkId) { return; } setCheck(checkId, true); } public void clearCheck() { setCheck(-1, false); } public int getCheckedRadioButtonId() { return this.checkId; } /** * 设置选中状态 */ private void setCheck(@IdRes int checkId, boolean isChecked) { if (checkId != -1 && this.checkId == checkId) { return; } if (checkId != -1) { CompoundButton view = (CompoundButton) findViewById(checkId); //未选中的RadioButton被选中 if (checkId != this.checkId && isChecked) { this.checkId = checkId; if (mChildOnCheckedChangeListener != null) { mChildOnCheckedChangeListener.onCheckedChanged(view, true); } //某个RadioButton被选中,将其他的改为未选中 for (int i = 0; i < getChildCount(); i++) { View v = getChildAt(i); if (v instanceof RadioButton && v.getId() != checkId) { ((RadioButton) v).setChecked(false); } else if (v instanceof RadioButton && v.getId() == checkId) { ((RadioButton) v).setChecked(true); } } } //被选中的RadioButton被取消选中 if (checkId == this.checkId && !isChecked) { this.checkId = checkId; if (mChildOnCheckedChangeListener != null) { mChildOnCheckedChangeListener.onCheckedChanged(view, false); } } } else { //清空所有选择 if (this.checkId != -1) { this.checkId = -1; CompoundButton view = (CompoundButton) findViewById(this.checkId); //将选中的置为未选中 if (view instanceof RadioButton) { view.setChecked(false); } } } } public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener) { this.mChildOnCheckedChangeListener = mChildOnCheckedChangeListener; } }
代码并没有太多,也很容易理解。有什么不明白的可以留言。
1、下载地址 : https://github.com/LuoChen-Hao/BlackHaoCustomView
总结
以上所述是小编给大家介绍的Android 自定义View实现任意布局的RadioGroup效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
- android自定义RadioGroup可以添加多种布局的实现方法
- Android自定义LinearLayout布局显示不完整的解决方法
- Android开发实现popupWindow弹出窗口自定义布局与位置控制方法
- Android简单实现自定义流式布局的方法
- Android自定义ViewGroup之实现FlowLayout流式布局
- Android开发之RadioGroup的简单使用与监听示例
- Android RadioGroup和RadioButton控件简单用法示例
- Android程序开发中单选按钮(RadioGroup)的使用详解
- Android控件系列之RadioButton与RadioGroup使用方法
- android RadioGroup的使用方法
相关文章推荐
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- Android 自定义ViewGroup实现整个Item布局竖直跑马灯效果
- android自定义RadioGroup实现可以添加多种布局
- Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
- Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
- android 实现FlowLayout 流线布局(自定义ViewGroup)
- android 自定义viewGroup实现网易左侧菜单效果
- android之自定义ViewGroup和自动换行的布局的实现
- Android自定义ViewGroup实现可滚动的横向布局(2)
- android之自定义ViewGroup和自动换行的布局的实现
- Android自定义ViewGroup:实现简单的垂直方向线性布局(2)
- android之自定义ViewGroup和自动换行的布局的实现
- Android动画效果之自定义ViewGroup添加布局动画
- Android UI设计之<十四>自定义ViewGroup,实现绚丽的仿支付宝咻一咻雷达脉冲效果
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- Android自定义控件系列六:自定义ViewGroup(一)实现ViewPager效果
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现