MainActivity如下:
package cc.testradiogroup; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.app.Activity; /** * Demo描述: * 利用自定义RadioGroup实现单选 * * 参考资料: * 1 http://blog.csdn.net/xiaanming * 2 http://bbs.51cto.com/thread-954128-1.html * * Thank you very much */ public class MainActivity extends Activity { private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mRadioGroup=(RadioGroup) findViewById(R.id.radioGroup); mRadioGroup.setOnCheckedChangeListener(new RadioButtonOnCheckedChangeListenerImpl()); } // 监听单选的变化 private class RadioButtonOnCheckedChangeListenerImpl implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb = (RadioButton) findViewById(group.getCheckedRadioButtonId()); String currentSelected = rb.getText().toString(); System.out.println("现在选中是:" + currentSelected); } } }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <!-- 步骤如下: --> <!--1: android:button="@null" 去掉自带的图标 --> <!--2:android:drawableRight="@drawable/line" 在文字的右边设置图片--> <!--3: android:drawablePadding="10dip" 图片与文字间的距离--> <!--4:android:layout_marginLeft="-17dip" 每个RadioButton距离左边缘或者距其左RadioButton的距离--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:text="测试自定义的RadioGroup来实现单选功能" > </TextView> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/bg" > <RadioButton android:id="@+id/cai" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-17dip" android:checked="true" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="菜" android:textColor="@drawable/text_selector" > </RadioButton> <RadioButton android:id="@+id/tang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="汤" android:textColor="@drawable/text_selector"> </RadioButton> <RadioButton android:id="@+id/zhushi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="主 食" android:textColor="@drawable/text_selector" > </RadioButton> <RadioButton android:id="@+id/zhou" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="15dip" android:text="粥" android:textColor="@drawable/text_selector" > </RadioButton> </RadioGroup> </LinearLayout>
text_selector.xml如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="#ffffff" android:state_checked="true"/> <item android:color="#000000"/> </selector>