当前位置:   article > 正文

android自定义RadioButton的样式_android radiobutton 自定义样式

android radiobutton 自定义样式

这里讲解RadioButton样式的实现、初始值、选择值得保存

一、样式的实现

            <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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

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>
  • 1
  • 2
  • 3
  • 4
  • 5

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二、给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);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、保存选中的值

    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;
            }
        }
    };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

自定义checkbox的样式,看这里

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/643929
推荐阅读
相关标签
  

闽ICP备14008679号