原文地址:https://blog.csdn.net/zxc514257857/article/details/60480014

仿苹果按钮效果

1、添加mavenCentral

在project的build.gradle—>buildscript—>repositories节点下添加mavenCentral() (:必须放在jcenter()前面)

buildscript {
    repositories {
        google()
        mavenCentral()  // 添加此句
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
  
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2、添加依赖

implementation 'com.github.zcweng:switch-button:[email protected]'

3、布局

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

4、MainActivity

switchButton = (SwitchButton) findViewById(R.id.switchButton);
switchButton.setOnCheckedChangeListener(this);

// 按钮监听事件
    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view.isChecked()) {
            switchButton.setChecked(true);
            Toast.makeText(this, "true", Toast.LENGTH_SHORT).show();
        } else {
            switchButton.setChecked(false);
            Toast.makeText(this, "false", Toast.LENGTH_SHORT).show();
        }
}

5、更多自定义玩法

布局代码
    <!--设置被打开-->
    <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"/>


**JAVA代码**
 @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);
        }

相关文章:

  • 2017-12-18
  • 2022-01-08
  • 2021-11-19
  • 2021-09-29
  • 2021-11-11
  • 2021-11-05
  • 2021-11-01
  • 2021-11-01
猜你喜欢
  • 2021-11-05
  • 2021-10-09
  • 2020-12-24
  • 2021-11-11
  • 2021-11-05
  • 2021-09-29
  • 2021-11-01
  • 2021-11-05
相关资源
相似解决方案