SwitchButton 开关按钮

Gradle:

dependencies {
	compile 'com.kyleduo.switchbutton:library:1.4.6'
}

XML:

<com.kyleduo.switchbutton.SwitchButton
    android:id="@+id/switch_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:textColor="@drawable/custom_text_state_color"
    app:kswTextOff="false"
    app:kswTextOn="true"
    app:kswThumbColor="#555771"
    app:kswThumbDrawable="@drawable/thumb_drawable"
    app:kswTintColor="#00ddB6"/>
 
  
@drawable/custom_text_state_color:
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:color="#ffffff" android:state_checked="true"/>
   <item android:color="@color/gray_color" android:state_checked="false"/>
selector>
@drawable/thumb_drawable:
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_enabled="false" android:state_checked="true">
      <shape android:shape="oval">
         <solid android:color="#E1E1E1"/>
         <stroke
            android:width="1px"
            android:color="#ECECEC"/>
      shape>
   item>
   <item android:state_enabled="false">
      <shape android:shape="oval">
         <solid android:color="#555771"/>
         <stroke
            android:width="1px"
            android:color="#55577E"/>
      shape>
   item>
   <item android:state_checked="true">
      <shape android:shape="oval">
         <solid android:color="#E1E1E1"/>
         <stroke
            android:width="1px"
            android:color="#D0D0D0"/>
      shape>
   item>
   <item>
      <shape android:shape="oval">
         <solid android:color="#555771"/>
         <stroke
            android:width="1px"
            android:color="#55577E"/>
      shape>
   item>
selector>

MainActivity:

SwitchButton switchButton = (SwitchButton) findViewById(R.id.switch_button);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            Logger.e("b", "开");
        } else {
            Logger.e("b", "关");
        }
    }
});
GitHub:https://github.com/kyleduo/SwitchButton



你可能感兴趣的:(开关按钮)