1. 程式人生 > >Android中仿iphone開關按鈕SwitchButton的基本使用

Android中仿iphone開關按鈕SwitchButton的基本使用

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'