自定义支持多行显示的RadioGroup
来源:互联网 发布:无线多重网络什么意思 编辑:程序博客网 时间:2024/05/16 06:59
自定义支持多行显示的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>
2 0
- 自定义支持多行显示的RadioGroup
- 支持自定义布局的RadioGroup
- android支持多行的radiogroup
- RadioGroup的多行显示RadioButton
- 【Android自定义控件】支持多层嵌套RadioButton的RadioGroup
- 【Android自定义控件】支持多层嵌套RadioButton的RadioGroup
- Android自定义控件】支持多层嵌套RadioButton的RadioGroup
- 自定义多行多列的RadioGroup
- 自定义多行RadioGroup控件实现记录
- 自定义RadioGroup实现多行排列
- Android RadioGroup 自定义布局, 支持多行多列
- Android RadioGroup 自定义布局, 支持多行多列
- RadioGroup的多行布局
- 自定义横向的RadioGroup:HorizontalRadioGroup
- 自定义的radioGroup和自定义的editText
- Android 动态生成 radiobutton 解决 radiogroup 多行显示的问题 单选
- Android 自定义View实现多行RadioGroup单选(多选)
- Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
- java基本数据类型与数组
- 侧滑手势与UIScrollView滚动手势冲突解决
- Tensorflow 官方版教程中文版
- 完全背包问题动态规划c++
- php ImageMagick imagick 图片处理
- 自定义支持多行显示的RadioGroup
- 大话设计模式总览
- 聚集索引和非聚集索引(整理)
- netfilter/iptables全攻略
- Cocos2d-x 3.x 场景切换
- android任意view移动缩放至消失到任何位置的通用动画
- 单点登录SSO的实现原理
- 白领的消亡
- 点击popupwindow窗口之外的区域popupwindow消失