Android中仿iphone开关按钮SwitchButton的基本使用

编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/60480014

Demo展示图片

这里写图片描述

添加依赖

  在project的build.gradle—>buildscript—>repositories节点下添加:

mavenCentral()

  如图:
这里写图片描述

  在module的dependencies节点下添加:

compile 'com.github.zcweng:switch-button:0.0.3@aar'

  如图:
这里写图片描述

布局代码

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <!--默认的switchbutton-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <!--设置被打开-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checked="true"/>

    <!--设置关闭选择器-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_show_indicator="false"/>

    <!--设置按钮阴影颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_shadow_color="#f00"/>

    <!--设置选中和未选中时的按钮背景颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checked_color="#f00"
        app:sb_uncheck_color="#0ff"
        app:sb_background="#0ff"/>

    <!--设置指示器选中和未选中时颜色及线宽-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checkline_color="#f00"
        app:sb_uncheckcircle_color="#0ff"
        app:sb_checkline_width="5dp"
        app:sb_uncheckcircle_width="5dp"/>

    <!--设置按钮颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_button_color="#f00"/>

    <!--设置是否启用开关特效-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_enable_effect="false"/>

</LinearLayout>

activity代码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.suke.widget.SwitchButton;
import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements SwitchButton.OnCheckedChangeListener {

    @Bind(R.id.switchButton1)
    SwitchButton mSwitchButton1;
    @Bind(R.id.switchButton2)
    SwitchButton mSwitchButton2;
    @Bind(R.id.switchButton3)
    SwitchButton mSwitchButton3;
    @Bind(R.id.switchButton4)
    SwitchButton mSwitchButton4;
    @Bind(R.id.switchButton5)
    SwitchButton mSwitchButton5;
    @Bind(R.id.switchButton6)
    SwitchButton mSwitchButton6;
    @Bind(R.id.switchButton7)
    SwitchButton mSwitchButton7;
    @Bind(R.id.switchButton8)
    SwitchButton mSwitchButton8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mSwitchButton1.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view.isChecked()){
            mSwitchButton2.setChecked(true);
            mSwitchButton3.setChecked(true);
            mSwitchButton4.setChecked(true);
            mSwitchButton5.setChecked(true);
            mSwitchButton6.setChecked(true);
            mSwitchButton7.setChecked(true);
            mSwitchButton8.setChecked(true);
        }else{
            mSwitchButton2.setChecked(false);
            mSwitchButton3.setChecked(false);
            mSwitchButton4.setChecked(false);
            mSwitchButton5.setChecked(false);
            mSwitchButton6.setChecked(false);
            mSwitchButton7.setChecked(false);
            mSwitchButton8.setChecked(false);
        }
    }
}

Demo中还依赖了:

compile 'com.jakewharton:butterknife:7.0.1'

  如想对SwitchButton布局进行更多修改,可移步SwitchButton的GitHub地址:https://github.com/zcweng/SwitchButton

Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9893023


----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamBackTo

感谢各位金主大大(* _ *)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值