SwitchButton 是 Android 上的一个开关按钮控件 【Deprecated】【Stop maintenance】

Related tags

Button switch-button
Overview

Logo SwitchButton

【Deprecated】【Stop maintenance】停止维护了,推荐使用:https://github.com/zcweng/SwitchButton

Android Arsenal Release Version

SwitchButton 是 Android 上的一个开关按钮控件

sample

示例 APP

SampleApp

扫描二维码或点我下载

特性

  • 支持滑动切换
  • 支持标题
  • 支持 Left、Top、Right、Bottom Drawable
  • 支持使用遮罩实现圆角按钮

使用指南(Usage Guide)

从 JCenter 导入

dependencies {
	compile 'me.panpf:switch-button:$lastVersionName'
}

$lastVersionNameRelease Version(不带v)

最低支持 Android2.2 API 7

在布局中使用

<?xml version="1.0" encoding="utf-8"?>
<me.panpf.switchbutton.SwitchButton
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="接受推送"/>

在代码中使用

SwitchButton 继承自 CompoundButton ,因此你可以像使用 CheckBox 那样设置状态和监听

一些可用的方法:

  • setDrawables(Drawable, Drawable, Drawable, Drawable):设置图片
  • setDrawableResIds(int, int, int, int):设置图片ID
  • setWithTextInterval(int):设置标题和按钮的间距,默认为 16dp
  • setDuration(int):设置动画持续时间,单位毫秒,默认为 200
  • setMinChangeDistanceScale(float):设置滑动有效距离比例,默认为 0.2。例如按钮宽度为 100,比例为 0.2,那么只有当滑动距离大于等于 (100*0.2) 才会切换状态,否则就回滚

自定义图片资源

SwitchButton 由四张图片构成

  • frameDrawable:框架图片,决定按钮的大小以及显示区域
  • stateDrawable:状态图片,显示开启或关闭状态
  • stateMaskDrawable:状态图片遮罩层,用于让状态图片按照遮罩层的形状显示
  • sliderDrawable:滑块图片

第一种方式你可以通过 SwitchButton 的自定义属性设置资源图片,如下:

<?xml version="1.0" encoding="utf-8"?>
<me.panpf.switchbutton.SwitchButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="接受推送"
    app:frameDrawable="@drawable/switch_frame"
    app:stateDrawable="@drawable/selector_switch_state"
    app:stateMaskDrawable="@drawable/switch_state_mask"
    app:sliderDrawable="@drawable/selector_switch_slider"/>

第二种方式是通过上面提到的 setDrawables(Drawable, Drawable, Drawable, Drawable) 或 setDrawableResIds(int, int, int, int) 方法设置资源图片

License

Copyright (C) 2017 Peng fei Pan <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
ToggleButton Widget For Android Dev
ToggleButton Widget For Android Dev

ToggleButton ToggleButton Widget For Android Developers @Deprecated !!!项目已经停止维护,新项目移至https://github.com/zcweng/SwitchButton !!! How To Use xml

Icons, Borders, Radius ... for Android buttons
Icons, Borders, Radius ... for Android buttons

⚠️ This library was made years ago when it wasn't that easy to customize Android buttons like today. I highly recommend you to use Material Design but

Android Floating ActionButton with a progress indicator ring
Android Floating ActionButton with a progress indicator ring

FabProgress Android Circular floating action button with intergrated progress indicator ring As per material design docs Demo: Demo apk HOW TO ADD TO

👏 The Medium's Clapping Effect developed in Android
👏 The Medium's Clapping Effect developed in Android

👏 MediumClap-Android Built with ❤︎ by Wajahat Karim and contributors A Custom Floating Action Button (FAB) library like clapping effect on Medium 📄

[] An Android library for an expandable button menu
[] An Android library for an expandable button menu

ExpandableButtonMenu ExpandableButtonMenu is an Android library which implements an expandable button that can be used as a substitute of a fixed size

This is a UI lib for Android. Effects like shining.
This is a UI lib for Android. Effects like shining.

ShineButton This is a UI lib for Android. Effects like shining. Usage shineButton = (ShineButton) findViewById(R.id.shine_button); shineButton.init(

A lightweight iOS switch view style for Android
A lightweight iOS switch view style for Android

iOS-SwitchView A lightweight iOS switch view style for Android Usage Add SwitchView into xml layout vn.luongvo.widget.iosswitchview.SwitchView an

SegmentedController is an Android UI library for using customizable RadioGroup with RadioButtons.
SegmentedController is an Android UI library for using customizable RadioGroup with RadioButtons.

SegmentedController is an Android UI library for using customizable RadioGroup with RadioButtons.

Android library providing an implementation of the Material Design Floating Action Button Speed Dial.
Android library providing an implementation of the Material Design Floating Action Button Speed Dial.

Android library providing an implementation of the Material Design Floating Action Button Speed Dial.

Comments
  • 有二个问题想请教

    有二个问题想请教

    hi,你写的代码很简洁,大赞。 有二个问题想请教下: 1,canvas.saveLayer可以过滤掉黑色边缘,这个原理是什么呢?求教! 2,绘制滑块层直接canvas.drawBitmap(sliderBitmap, tempSlideX, 0, paint);也可以达到预期效果,想问下你在处理绘制滑块层也使用canvas.saveLayer和Xfermode的目的是什么呢?

    opened by zhuchen1109 3
  • 你好,請問怎麼重置按鈕狀態?

    你好,請問怎麼重置按鈕狀態?

    public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b == false) { // TODO: 取消事件處理 compoundButton.setChecked(true); } }

    我想做個專用於取消的滑動按鈕,每次滑動完(處理取消事件後)都必須回到按鈕的預設狀態。 但是以上的方法,Button的Checked會回到true沒有錯,但是圖片會變成錯誤的狀態。

    opened by KawaHu 1
  • SwitchButton的setOnCheckedChangeListener问题

    SwitchButton的setOnCheckedChangeListener问题

    你好,请教一个问题: 我用SwitchButton的setOnCheckedChangeListener来执行相应的方法。 如: mSwitch.setOnCheckedChangeListener((new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked == false)
                    {
    
                    }
                    else
                    {
    
                    }
               }
        }));
    

    问题: 我点击一次SwitchButton,但是onCheckedChanged却调用多次,有时候一次,有时候多次。请问哪里可以设置或者修改吗?

    opened by iacxin099 0
  • setOnClickListener is not called when widget is clicked

    setOnClickListener is not called when widget is clicked

    It is a common use to use OnClickListener when you need to detect if the check change is a user change or a program change (e.g., setCheck()).

    SInce OnClickListener is not called this common design pattern cannot work with the control.

    opened by kenargo 2
Owner
Nameless
Android program ape
Nameless
Android Circular Progress Button

?? Before using this library, read information below ?? This library is not more supported. If you want to add new feature or fix a bug, grab source

Dmytro Danylyk 5.6k Jan 7, 2023
Android Buttons With Built-in Progress Meters.

Description Android Buttons With Built-in Progress Meters. Wiki Home Screenshots User Guide Integration The lib is available on Maven Central, you can

Dmytro Danylyk 3k Dec 29, 2022
Circle button widget for Android

DEPRECATED This library is deprecated and no new development is taking place. Consider using a FAB(Floating action button) instead. E.g. the Android D

Markus Hintersteiner 1.5k Dec 9, 2022
FButton - a flat button library for Android

FButton FButton is a custom Button of Android with "Flat UI" concept. FButton's design get inspiration from designmono. This library is very small and

Le Van Hoang 1.4k Dec 12, 2022
Android button which moves in eight direction.

Moving Button Android button which moves in eight direction. Preview Sample Demo You can download demo movie file here : demo.mov It's also on Youtube

Leonardo Taehwan Kim 130 Nov 22, 2022
Button for android with animations for transition and error states.

Transition Button Android Preview Expand animation: Shake animation: Installation Gradle dependencies { implementation 'com.royrodriguez:transitionbu

Roy Rodriguez 137 Jan 3, 2023
AwesomeSwitch is a replacement for the standard Switch(View) android offers, and it offers much more customization than the standard switch component.

AwesomeSwitch AwesomeSwitch is a replacement for the standard Switch(View) android offers, and it offers much more customization than the standard swi

Anoop S S 29 Jun 2, 2022
[] Android floating action button

DEPRECATED Use the FloatingActionButton from the support library instead. FloatingActionButton Description Android floating action button which reacts

Oleksandr Melnykov 4k Jan 5, 2023
Floating Action Button for Android based on Material Design specification

FloatingActionButton Yet another library for drawing Material Design promoted actions. Features Support for normal 56dp and mini 40dp buttons. Customi

Zendesk 6.4k Dec 26, 2022
Bootstrap style widgets for Android, with Glyph Icons

Android-Bootstrap Android Bootstrap is an Android library which provides custom views styled according to the Twitter Bootstrap Specification. This al

Bearded Hen 7.3k Jan 3, 2023