android自定义radiogroup,自定义RadioGroup

自定义RadioGroup

在Android系统中,自带的RadioGroup只能指定横向和纵向两种布局,所以有的时候我们需要自定义RadioGroup。

首先分析一下,就是在系统自带的RadioGroup中,如果我们嵌套了,LinearLayout的话,就会失效,因为系统的RadioGroup没有考虑到这种情况,所以我们需要自定义一个Group,初步的打算是继承自LinearLayout。

具体代码如下:

package linsir.fuyizhulao.com.love_map;

import android.content.Context;

import android.content.res.TypedArray;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.accessibility.AccessibilityEvent;

import android.view.accessibility.AccessibilityNodeInfo;

import android.widget.CompoundButton;

import android.widget.LinearLayout;

import android.widget.RadioButton;

/**

*

This class is used to create a multiple-exclusion scope for a set of radio

* buttons. Checking one radio button that belongs to a radio group unchecks

* any previously checked radio button within the same group.

*

*

Intially, all of the radio buttons are unchecked. While it is not possible

* to uncheck a particular radio button, the radio group can be cleared to

* remove the checked state.

*

*

The selection is identified by the unique id of the radio button as defined

* in the XML layout file.

*

*

XML Attributes

*

See {@link android.R.styleable#RadioGroup RadioGroup Attributes},

* {@link android.R.styleable#LinearLayout LinearLayout Attributes},

* {@link android.R.styleable#ViewGroup ViewGroup Attributes},

* {@link android.R.styleable#View View Attributes}

*

Also see

* {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}

* for layout attributes.

*

* @see RadioButton

*

*/

public class RadioGroup extends LinearLayout {

// holds the checked id; the selection is empty by default

private int mCheckedId = -1;

// tracks children radio buttons checked state

private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;

// when true, mOnCheckedChangeListener discards events

private boolean mProtectFromCheckedChange = false;

private OnCheckedChangeListener mOnCheckedChangeListener;

private PassThroughHierarchyChangeListener mPassThroughListener;

/**

* {@inheritDoc}

*/

public RadioGroup(Context context) {

super(context);

setOrientation(VERTICAL);

init();

}

/**

* {@inheritDoc}

*/

public RadioGroup(Context context, AttributeSet attrs) {

super(context, attrs);

mCheckedId = View.NO_ID;

final int index = VERTICAL;

setOrientation(index);

init();

}

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值