一、废话不多说,先上图
二、实现思路
如果你们的效果要求不是太严苛,你可以使用这种方式。看起来还是挺美观和漂亮的。
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
android:shape="oval">
android:width="30dp"
android:height="30dp">
android:color="@android:color/white">
switch_track.xml
android:state_checked="true"
android:drawable="@drawable/switch_track_on"/>
android:state_checked="false"
android:drawable="@drawable/switch_track_off"/>
switch_track_off.xml
android:shape="rectangle">
android:color="#e4e4e4">
android:radius="30dp">
switch_track_on.xml<?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();
}
}
});