一、样式的实现
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="开启支付"
android:textColor="@color/black"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/rgPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbYes"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:background="@drawable/radiobutton_selector"
android:button="@null"
android:gravity="center"
android:text="是"
android:textColor="@drawable/radiobutton_text_color"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rbNo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="@drawable/radiobutton_selector"
android:button="@null"
android:gravity="center"
android:text="否"
android:textColor="@drawable/radiobutton_text_color"
android:textSize="18sp" />
</RadioGroup>
radiobutton_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_radiobutton_n" android:state_checked="false" />
<item android:drawable="@drawable/shape_radiobutton_p" android:state_checked="true" />
</selector>
shape_radiobutton_n
<?xml version="1.0" encoding="utf-8"?><!-- 实线边框+内部填充 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="true">
<corners android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<!--描边-->
<stroke android:color="#07ACED"
android:width="2dp"></stroke>
<!--实心-->
<solid android:color="@color/white" />
<padding android:bottom="10dp"
android:left="10dp"
android:top="10dp"
android:right="10dp"/>
</shape>
shape_radiobutton_p
<?xml version="1.0" encoding="utf-8"?><!-- 实线边框+内部填充 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="true">
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<!--描边-->
<stroke
android:width="2dp"
android:color="#07ACED"></stroke>
<!--实心-->
<solid android:color="#07ACED" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
radiobutton_text_color
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:color="@color/color_text_00aeef" />
<item
android:state_checked="true"
android:color="@color/white" />
</selector>
二、给RadioButton赋值
boolean isOpen = SPUtils.getInstance().getBoolean(Const.SPKEY.PAY_IS_PAY,true);
if (isOpen) {
RadioButton radioButton1 = (RadioButton) rgPay.getChildAt(0);
radioButton1.setChecked(true);
} else {
RadioButton radioButton2 = (RadioButton) rgPay.getChildAt(1);
radioButton2.setChecked(true);
}
三、保存选中的值
private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rbYes:
SPUtils.getInstance().put(Const.SPKEY.PAY_IS_PAY, true);
break;
case R.id.rbNo:
SPUtils.getInstance().put(Const.SPKEY.PAY_IS_PAY, false);
break;
}
}
};
自定义checkbox的样式,看这里