自定义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();
}
<