android:layout_width="wrap_content" android:_自定义radiobutton">
赞
踩
有时候,我们需要多个Button控件,而且这些控件互相限制,这个时候,我们就可以想到RadioGroup。以下为部分代码,附带着demo源代码。
1、自定义RadioButton
package com.shao.button.buttonlistener;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioGroup;
public class ButtonRadioGroup extends RadioGroup {
public ButtonRadioGroup(Context context) {
super(context);
}
public ButtonRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
changeButtonsImages();
}
private void changeButtonsImages(){
int count = super.getChildCount();
if(count > 1){
super.getChildAt(0).setBackgroundResource(R.drawable.absence_radio);
for(int i=1; i < count-1; i++){
super.getChildAt(i).setBackgroundResource(R.drawable.ask_radio);
}
super.getChildAt(count-1).setBackgroundResource(R.drawable.late_radio);
}else if (count == 1){
super.getChildAt(0).setBackgroundResource(R.drawable.init_button);
}
}
}
2、自定义Adapter布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</LinearLayout>
源代码下载:ButtonListener.zip
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。