[android]代码库
package com.example.myradiobtn;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
/**
 * 控件
 * 
 * @author Luo.Yunlongx
 * 
 */
public class MyChoose extends LinearLayout {
	private Context mContext;
	private View mView;
	private RadioGroup mRadioGroup;
	private RadioButton mLeftRadioButton;
	private RadioButton mRightRadioButton;
	private LayoutParams mLayoutParams;
	/**
	 * 构造方法
	 * 
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MyChoose(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		this.mContext = context;
		init();
	}
	/**
	 * 构造方法
	 * 
	 * @param context
	 * @param attrs
	 */
	public MyChoose(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();
	}
	/**
	 * 构造方法
	 * 
	 * @param context
	 */
	public MyChoose(Context context) {
		super(context);
		this.mContext = context;
		init();
	}
	/**
	 * 初始化
	 */
	private void init() {
		mView = LayoutInflater.from(mContext).inflate(R.layout.view_mychoose,
				null);
		mLeftRadioButton = (RadioButton) mView
				.findViewById(R.id.mychoose_radioButton1);
		mRightRadioButton = (RadioButton) mView
				.findViewById(R.id.mychoose_radioButton2);
		mRadioGroup = (RadioGroup) mView.findViewById(R.id.mychoose_radioGroup);
		mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mView.setLayoutParams(mLayoutParams);
		this.setLayoutParams(mLayoutParams);
		this.addView(mView);
	}
	/**
	 * 设置显示的文字
	 * 
	 * @param leftText
	 *            左侧文字
	 * @param rightText
	 *            右侧文字
	 */
	public void setTexts(String leftText, String rightText) {
		mLeftRadioButton.setText(leftText);
		mRightRadioButton.setText(rightText);
	}
	/**
	 * 设置点击事件
	 * 
	 * @param mOnCheckedChange
	 */
	public void setOnClick(final IOnCheckedChange mOnCheckedChange, final int id) {
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.mychoose_radioButton1:
					/* 改变字的颜色 */
					mLeftRadioButton.setTextColor(mContext.getResources()
							.getColor(android.R.color.white));
					mRightRadioButton.setTextColor(mContext.getResources()
							.getColor(android.R.color.black));
					/**
					 * 选中左侧时的事件
					 */
					if (mOnCheckedChange != null) {
						mOnCheckedChange.leftOnClick(id);
					}
					break;
				case R.id.mychoose_radioButton2:
					/* 改变字的颜色 */
					mRightRadioButton.setTextColor(mContext.getResources()
							.getColor(android.R.color.white));
					mLeftRadioButton.setTextColor(mContext.getResources()
							.getColor(android.R.color.black));
					/**
					 * 选中右侧时的事件
					 */
					if (mOnCheckedChange != null) {
						mOnCheckedChange.rightOnClick(id);
					}
					break;
				}
			}
		});
	}
	/**
	 * 是否第一个选中
	 * 
	 * @param leftSelected
	 */
	public void setChoose(boolean leftSelected) {
		mLeftRadioButton.setSelected(leftSelected);
		mRightRadioButton.setSelected(!leftSelected);
	}
	/**
	 * 
	 * 自己写个接口,处理RadioGroup.setOnCheckedChangeListener相应
	 * 
	 * @author Luo.Yunlongx
	 * 
	 */
	public interface IOnCheckedChange {
		/**
		 * 左侧被选中
		 * 
		 * @param id
		 *            区分事件源
		 */
		public void leftOnClick(int id);
		/**
		 * 右侧被选中
		 * 
		 * @param id
		 *            区分事件源
		 */
		public void rightOnClick(int id);
	}
}
[代码运行效果截图]
[源代码打包下载]