自定义支持多行显示的RadioGroup
2016-02-18 11:47
381 查看
自定义支持多行显示的RadioGroup
原生的RadioGroup继承自LinearLayout,即只能支持一横排或者一竖排的排列显示RadioButton现在改写RadioGroup,使它支持多行多列排布RadioButton,效果图如下
效果图
重写RadioGroup
package com.kongqw.radiogroupdemo; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; /** * Created by kongqw on 2016/2/18. */ public class MyRadioGroup extends RadioGroup { private OnCheckedChangeListener mOnCheckedChangeListener; public MyRadioGroup(Context context) { super(context); } public MyRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); } public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { mOnCheckedChangeListener = listener; } @Override public void addView(final View child, int index, ViewGroup.LayoutParams params) { if (child instanceof LinearLayout) { int childCount = ((LinearLayout) child).getChildCount(); for (int i = 0; i < childCount; i++) { View view = ((LinearLayout) child).getChildAt(i); if (view instanceof RadioButton) { final RadioButton button = (RadioButton) view; ((RadioButton) button).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((RadioButton) button).setChecked(true); checkRadioButton((RadioButton) button); if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId()); } return true; } }); } } } super.addView(child, index, params); } private void checkRadioButton(RadioButton radioButton) { View child; int radioCount = getChildCount(); for (int i = 0; i < radioCount; i++) { child = getChildAt(i); if (child instanceof RadioButton) { if (child == radioButton) { // do nothing } else { ((RadioButton) child).setChecked(false); } } else if (child instanceof LinearLayout) { int childCount = ((LinearLayout) child).getChildCount(); for (int j = 0; j < childCount; j++) { View view = ((LinearLayout) child).getChildAt(j); if (view instanceof RadioButton) { final RadioButton button = (RadioButton) view; if (button == radioButton) { // do nothing } else { ((RadioButton) button).setChecked(false); } } } } } } }
布局
RadioGroup里面还可以包裹一层LinearLayout<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.kongqw.radiogroupdemo.MainActivity"> <com.kongqw.radiogroupdemo.MyRadioGroup android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C++" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OC" /> </LinearLayout> </com.kongqw.radiogroupdemo.MyRadioGroup> </RelativeLayout>
相关文章推荐
- mysql如何将多行数据合并成一行
- android自定义RadioGroup可以添加多种布局的实现方法
- Android控件系列之RadioButton与RadioGroup使用方法
- oracle实现多行合并的方法
- Android编程开发之RadioGroup用法实例
- HTML前端开发之路——多列布局
- 关于RadioGroup的xml文件
- 去掉radioGroup的图标
- cocos2dx radiogrouop实现
- 探索RadioGroup下可否添加子Layout
- android sdk---radioGroup用法实例
- android sdk --- radioGroup的选择保存学习以及animation学习
- oracle多行合并一行
- excel 多列内容合并为一个单元格
- Android 自定义View实现多行RadioGroup单选(多选)
- 仿淘宝实现多行星级评价
- vim 多行缩进
- Silverlight datagrid定制,实现 每行可细分为若干小行
- [iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
- Android开发:CompoundButton.onCheckedChangeListener和RadioGroup.onCheckedChangeListener冲突问题