android控件-Switch使用

1、布局代码

<Switch
        android:id="@+id/switchs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/width_15"
        android:showText="false"
        android:switchMinWidth="@dimen/width_41"
        android:thumb="@drawable/switch_thumb"
        android:track="@drawable/switchs_bg"
        app:layout_constraintBottom_toBottomOf="@id/defaultAddressText"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/defaultAddressText" />

2、添加开关按钮的圆点背景switch_thumb

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_thumb_select" android:state_checked="true" />
    <item android:drawable="@drawable/switch_thumb_unselect" android:state_checked="false" />
</selector>

3、添加开关按钮圆点选中的背景色switch_thumb_select

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/white" />
    <stroke android:color="@color/color_E5E5E5"
        android:width="@dimen/width_1"/>
    <size
        android:width="@dimen/width_25"
        android:height="@dimen/width_25" />
</shape>

4、添加开关按钮圆点未选中的背景色sswitch_thumb_unselect

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/white" />
    <stroke android:color="@color/color_E5E5E5"
        android:width="@dimen/width_1"/>
    <size
        android:width="@dimen/width_25"
        android:height="@dimen/width_25" />
</shape>

5、添加开关按钮的背景switchs_bg

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switchs_on" android:state_checked="true" />
    <item android:drawable="@drawable/switchs_off"  android:state_checked="false"/>
</selector>

6、添加开关按钮的选中背景switchs_on

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_4CD964" />
    <corners android:radius="@dimen/width_23" />
    <stroke android:color="@color/color_E5E5E5"
        android:width="@dimen/width_1"/>
    <size android:height="@dimen/width_25" />
</shape>

7、添加开关按钮的未选中背景switchs_off

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners android:radius="@dimen/width_23" />
    <stroke
        android:width="@dimen/width_1"
        android:color="@color/color_E5E5E5" />
    <size android:height="@dimen/width_25" />
</shape>

 

8、最后在代码中选择未选中的使用方式

// 默认选中状态 false为未选中 true为选中
switchs.setChecked(true);
// 开关按钮的点击事件
switchs.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                //选中
            } else {
            }
        });

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值