android 高仿ios开关,Android 高仿IOS打开和关闭按钮

一、废话不多说,先上图

FqSPIRFhw274f6dwMl3UwBXFiqZA

二、实现思路

如果你们的效果要求不是太严苛,你可以使用这种方式。看起来还是挺美观和漂亮的。

1、使用系统的Switch控件,然后设置thumb和track背景就能实现

2、然后监听setOnCheckedChangeListener就能知道打开和关闭

三、代码

activity.xml

android:id="@+id/activity_setServe_switch_open"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textOff=""

android:layout_marginTop="100dp"

android:layout_marginLeft="100dp"

android:textOn=""

android:thumb="@drawable/switch_thumb"

android:track="@drawable/switch_track" />switch_thumb.xml

FsVG3V54OHhjOsoGJ6z_sAKX8a7Z

android:shape="oval">

android:width="30dp"

android:height="30dp">

android:color="@android:color/white">

switch_track.xml

Fr2KjzWGpA08Q3lOWYW3PlvbXiNS

android:state_checked="true"

android:drawable="@drawable/switch_track_on"/>

android:state_checked="false"

android:drawable="@drawable/switch_track_off"/>

switch_track_off.xml

Fr2KjzWGpA08Q3lOWYW3PlvbXiNS

android:shape="rectangle">

android:color="#e4e4e4">

android:radius="30dp">

switch_track_on.xml

FkPC00JvYlILFXnm8v0Tml1oWu_Q<?xml version="1.0" encoding="utf-8"?>

android:shape="rectangle">

android:color="#00ABEC">

android:radius="32dp">

最后设置监听事件

((Switch)findViewById(R.id.activity_setServe_switch_open)).

setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

Toast.makeText(MainActivity.this,"打开",Toast.LENGTH_LONG).show();

} else {

Toast.makeText(MainActivity.this,"关闭",Toast.LENGTH_LONG).show();

}

}

});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值