Android自定义View之RadioGroup实现跨多行显示
本文实例为大家分享了AndroidRadioGroup跨多行显示的具体代码,供大家参考,具体内容如下
此自定义View源于网络,具体出处不详。
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.util.AttributeSet;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.accessibility.AccessibilityEvent;
importandroid.view.accessibility.AccessibilityNodeInfo;
importandroid.widget.CompoundButton;
importandroid.widget.LinearLayout;
importandroid.widget.RadioButton;
publicclassRadioGroupextendsLinearLayout{
//holdsthecheckedid;theselectionisemptybydefault
privateintmCheckedId=-1;
//trackschildrenradiobuttonscheckedstate
privateCompoundButton.OnCheckedChangeListenermChildOnCheckedChangeListener;
//whentrue,mOnCheckedChangeListenerdiscardsevents
privatebooleanmProtectFromCheckedChange=false;
privateOnCheckedChangeListenermOnCheckedChangeListener;
privatePassThroughHierarchyChangeListenermPassThroughListener;
/**
*{@inheritDoc}
*/
publicRadioGroup(Contextcontext){
super(context);
setOrientation(VERTICAL);
init();
}
/**
*{@inheritDoc}
*/
publicRadioGroup(Contextcontext,AttributeSetattrs){
super(context,attrs);
mCheckedId=View.NO_ID;
finalintindex=VERTICAL;
setOrientation(index);
init();
}
privatevoidinit(){
mChildOnCheckedChangeListener=newCheckedStateTracker();
mPassThroughListener=newPassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
/**
*{@inheritDoc}
*/
@Override
publicvoidsetOnHierarchyChangeListener(OnHierarchyChangeListenerlistener){
//theuserlistenerisdelegatedtoourpass-throughlistener
mPassThroughListener.mOnHierarchyChangeListener=listener;
}
/**
*{@inheritDoc}
*/
@Override
protectedvoidonFinishInflate(){
super.onFinishInflate();
//checkstheappropriateradiobuttonasrequestedintheXMLfile
if(mCheckedId!=-1){
mProtectFromCheckedChange=true;
setCheckedStateForView(mCheckedId,true);
mProtectFromCheckedChange=false;
setCheckedId(mCheckedId);
}
}
@Override
publicvoidaddView(finalViewchild,intindex,ViewGroup.LayoutParamsparams){
if(childinstanceofRadioButton){
child.setOnTouchListener(newOnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
((RadioButton)child).setChecked(true);
checkRadioButton((RadioButton)child);
if(mOnCheckedChangeListener!=null){
mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this,child.getId());
}
returntrue;
}
});
}elseif(childinstanceofLinearLayout){
intchildCount=((LinearLayout)child).getChildCount();
for(inti=0;iSetstheselectiontotheradiobuttonwhoseidentifierispassedin
*parameter.Using-1astheselectionidentifierclearstheselection;
*suchanoperationisequivalenttoinvoking{@link#clearCheck()}.
*
*@paramidtheuniqueidoftheradiobuttontoselectinthisgroup
*@see#getCheckedRadioButtonId()
*@see#clearCheck()
*/
publicvoidcheck(intid){
//don'tevenbother
if(id!=-1&&(id==mCheckedId)){
return;
}
if(mCheckedId!=-1){
setCheckedStateForView(mCheckedId,false);
}
if(id!=-1){
setCheckedStateForView(id,true);
}
setCheckedId(id);
}
privatevoidsetCheckedId(intid){
mCheckedId=id;
}
privatevoidsetCheckedStateForView(intviewId,booleanchecked){
ViewcheckedView=findViewById(viewId);
if(checkedView!=null&&checkedViewinstanceofRadioButton){
((RadioButton)checkedView).setChecked(checked);
}
}
/**
*Returnstheidentifieroftheselectedradiobuttoninthisgroup.
*Uponemptyselection,thereturnedvalueis-1.
*
*@returntheuniqueidoftheselectedradiobuttoninthisgroup
*@attrrefandroid.R.styleable#RadioGroup_checkedButton
*@see#check(int)
*@see#clearCheck()
*/
publicintgetCheckedRadioButtonId(){
returnmCheckedId;
}
/**
*Clearstheselection.Whentheselectioniscleared,noradiobutton
*inthisgroupisselectedand{@link#getCheckedRadioButtonId()}returns
*null.
*
*@see#check(int)
*@see#getCheckedRadioButtonId()
*/
publicvoidclearCheck(){
check(-1);
}
/**
*Registeracallbacktobeinvokedwhenthecheckedradiobutton
*changesinthisgroup.
*
*@paramlistenerthecallbacktocalloncheckedstatechange
*/
publicvoidsetOnCheckedChangeListener(OnCheckedChangeListenerlistener){
mOnCheckedChangeListener=listener;
}
/**
*{@inheritDoc}
*/
@Override
publicLayoutParamsgenerateLayoutParams(AttributeSetattrs){
returnnewRadioGroup.LayoutParams(getContext(),attrs);
}
/**
*{@inheritDoc}
*/
@Override
protectedbooleancheckLayoutParams(ViewGroup.LayoutParamsp){
returnpinstanceofRadioGroup.LayoutParams;
}
@Override
protectedLinearLayout.LayoutParamsgenerateDefaultLayoutParams(){
returnnewLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
@Override
publicvoidonInitializeAccessibilityEvent(AccessibilityEventevent){
super.onInitializeAccessibilityEvent(event);
event.setClassName(RadioGroup.class.getName());
}
@Override
publicvoidonInitializeAccessibilityNodeInfo(AccessibilityNodeInfoinfo){
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(RadioGroup.class.getName());
}
publicstaticclassLayoutParamsextendsLinearLayout.LayoutParams{
/**
*{@inheritDoc}
*/
publicLayoutParams(Contextc,AttributeSetattrs){
super(c,attrs);
}
/**
*{@inheritDoc}
*/
publicLayoutParams(intw,inth){
super(w,h);
}
/**
*{@inheritDoc}
*/
publicLayoutParams(intw,inth,floatinitWeight){
super(w,h,initWeight);
}
/**
*{@inheritDoc}
*/
publicLayoutParams(ViewGroup.LayoutParamsp){
super(p);
}
/**
*{@inheritDoc}
*/
publicLayoutParams(MarginLayoutParamssource){
super(source);
}
/**
*Fixesthechild'swidthto
*{@linkandroid.view.ViewGroup.LayoutParams#WRAP_CONTENT}andthechild's
*heightto{@linkandroid.view.ViewGroup.LayoutParams#WRAP_CONTENT}
*whennotspecifiedintheXMLfile.
*
*@paramathestyledattributesset
*@paramwidthAttrthewidthattributetofetch
*@paramheightAttrtheheightattributetofetch
*/
@Override
protectedvoidsetBaseAttributes(TypedArraya,intwidthAttr,intheightAttr){
if(a.hasValue(widthAttr)){
width=a.getLayoutDimension(widthAttr,"layout_width");
}else{
width=WRAP_CONTENT;
}
if(a.hasValue(heightAttr)){
height=a.getLayoutDimension(heightAttr,"layout_height");
}else{
height=WRAP_CONTENT;
}
}
}
/**
*Interfacedefinitionforacallbacktobeinvokedwhenthechecked
*radiobuttonchangedinthisgroup.
*/
publicinterfaceOnCheckedChangeListener{
/**
*Calledwhenthecheckedradiobuttonhaschanged.Whenthe
*selectioniscleared,checkedIdis-1.
*
*@paramgroupthegroupinwhichthecheckedradiobuttonhaschanged
*@paramcheckedIdtheuniqueidentifierofthenewlycheckedradiobutton
*/
voidonCheckedChanged(RadioGroupgroup,intcheckedId);
}
privateclassCheckedStateTrackerimplementsCompoundButton.OnCheckedChangeListener{
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
//preventsfrominfiniterecursion
if(mProtectFromCheckedChange){
return;
}
mProtectFromCheckedChange=true;
if(mCheckedId!=-1){
setCheckedStateForView(mCheckedId,false);
}
mProtectFromCheckedChange=false;
intid=buttonView.getId();
setCheckedId(id);
}
}
/**
*Apass-throughlisteneractsupontheeventsanddispatchesthem
*toanotherlistener.Thisallowsthetablelayouttosetitsowninternal
*hierarchychangelistenerwithoutpreventingtheusertosetuphis.
*/
privateclassPassThroughHierarchyChangeListenerimplementsViewGroup.OnHierarchyChangeListener{
privateViewGroup.OnHierarchyChangeListenermOnHierarchyChangeListener;
/**
*{@inheritDoc}
*/
publicvoidonChildViewAdded(Viewparent,Viewchild){
if(parent==RadioGroup.this&&childinstanceofRadioButton){
intid=child.getId();
//generatesanidifit'smissing
if(id==View.NO_ID){
id=child.hashCode();
child.setId(id);
}
((RadioButton)child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);
}
if(mOnHierarchyChangeListener!=null){
mOnHierarchyChangeListener.onChildViewAdded(parent,child);
}
}
/**
*{@inheritDoc}
*/
publicvoidonChildViewRemoved(Viewparent,Viewchild){
if(parent==RadioGroup.this&&childinstanceofRadioButton){
((RadioButton)child).setOnCheckedChangeListener(null);
}
if(mOnHierarchyChangeListener!=null){
mOnHierarchyChangeListener.onChildViewRemoved(parent,child);
}
}
}
}
使用:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。