RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色

时间:2021-12-07 01:46:14

RadioGroup

  1. <RadioGroup
  2. android:id="@+id/client_charge_radiogroup"
  3. android:layout_width="200dp"
  4. android:layout_height="40dp"
  5. android:layout_marginLeft="5dp"
  6. android:layout_alignParentRight="true"
  7. android:fadingEdge="none"
  8. android:gravity="center_vertical"
  9. android:layout_centerVertical="true"
  10. android:orientation="horizontal"
  11. >
  12. <RadioButton
  13. android:id="@+id/client_radio_label"
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_weight="1"
  17. android:button="@color/transparent"
  18. android:gravity="center"
  19. android:text="Client"
  20. android:textColor="@color/color_radiobutton"
  21. android:background="@drawable/radio_group_selector"
  22. />
  23. <RadioButton
  24. android:id="@+id/firm_radio_label"
  25. android:layout_width="0dp"
  26. android:layout_height="match_parent"
  27. android:layout_weight="1"
  28. android:button="@color/transparent"
  29. android:gravity="center"
  30. android:text="Firm"
  31. android:checked="true"
  32. android:textColor="@color/color_radiobutton"
  33. android:background="@drawable/radio_group_selector"/>
  34. </RadioGroup>

radiobutton字体颜色改变color_radiobutton.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="true" android:color="@color/color_text_selected"/>
  4. <!-- not selected -->
  5. <item android:state_checked="false" android:color="@color/color_text_normal"/>
  6. </selector>

radiobutton背景颜色改变radio_group_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_checked="true" android:drawable="@color/color_bg_selected" ></item>
  4. <item android:state_checked="false" android:drawable="@color/color_bg_normal"></item>
  5. </selector>

color.xml

  1. <color name="transparent">#000000</color>
  2. <color name="color_bg_selected">#e0301e</color>
  3. <color name="color_bg_normal">#e7e7e8</color>
  4. <color name="color_text_selected">#ffffff</color>
  5. <color name="color_text_normal">#000000</color>

Activity

    1. clientRadioGroup = (RadioGroup) findViewById(R.id.client_charge_radiogroup);
    2. clientRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    3. @Override
    4. public void onCheckedChanged(RadioGroup group, int checkedId) {
    5. int radioButtonId = group.getCheckedRadioButtonId();
    6. RadioButton rb = (RadioButton)findViewById(radioButtonId);
    7. String radioButtonLabel = rb.getText().toString();
    8. }
    9. });

http://blog.csdn.net/zzf112/article/details/20467957

--------------------->

android:background="@drawable/drawlist"--------->不能用colorlist

radiogroup------->radiobutton必须要有id否则会两个都选中

    <RadioGroup
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" > <RadioButton
android:id="@+id/radio0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/zuowei"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="座位安排"
android:textColor="#ffffff" /> <RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/zuowei"
android:button="@null"
android:gravity="center"
android:text="菜单编辑"
android:textColor="#ffffff" />
</RadioGroup>