Switch 和 ToggleButton开关控件详解

之前看到有人还是用textview或imageview来实现开关功能,确实可以实现,但是有点落后了,谷歌提供了两个开关控件Switch和ToggleButton,下面介绍用法

结构

首先两个控件都是继承CompoundButton,和CheckBox出自一家,都是有切换效果

public class Switch extends CompoundButton 

java.lang.Object
   ↳	android.view.View
 	   ↳	android.widget.TextView
 	 	   ↳	android.widget.Button
 	 	 	   ↳	android.widget.CompoundButton
 	 	 	 	   ↳	android.widget.Switch
public class ToggleButton extends CompoundButton 

java.lang.Object
   ↳	android.view.View
 	   ↳	android.widget.TextView
 	 	   ↳	android.widget.Button
 	 	 	   ↳	android.widget.CompoundButton
 	 	 	 	   ↳	android.widget.ToggleButton

基本属性

ToggleButton
  • android:disabledAlpha:不可用时的透明度
  • android:textOff:关闭时的文字
  • android:textOn:打开时的文字
Switch
  • android:showText:设置on/off的时候是否显示文字,boolean
  • android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean
  • android:switchMinWidth:设置开关的最小宽度
  • android:switchPadding:设置滑块内文字的间隔
  • android:switchTextAppearance:设置开关的文字外观
  • android:textOff:关闭时的文字
  • android:textOn:打开时的文字
  • android:textStyle:文字风格,粗体,斜体写划线那些
  • android:thumb:背景
  • android:thumbTextPadding:设置背景和文字之间的距离
  • android:thumbTint:滑动块颜色
  • android:thumbTintMode:滑动块颜色模式
  • android:track:切换图片
  • android:trackTint:切换颜色
  • android:trackTintMode:切换模式
  • android:typeface:设置字体

xml定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff=""
        android:textOn=""
        />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff=""
        android:textOn=""
        />
</LinearLayout>

在这里插入图片描述

监听事件

ToggleButton toggleButton = findViewById(R.id.togglebutton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){

                }else {

                }
            }
        });

        Switch aSwitch = findViewById(R.id.aswitch);
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){

                }else {

                }
            }
        });

自定义样式

功能是实现了,但是实际开发还需要改变一下样式

ToggleButton
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:textOff=""
        android:textOn=""
        android:background="@android:color/transparent"
        android:button="@drawable/switch_selector"
         />
Switch
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:switchMinWidth="30dp"
        android:checked="true"
        android:thumb="@android:color/transparent"
        android:track="@drawable/switch_selector"
        />

switch_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_close" android:state_checked="false" />
    <item android:drawable="@drawable/switch_open" android:state_checked="true" />
</selector>

在这里插入图片描述

注意事项:

  1. 使用switch自定义样式时需要设置android:switchMinWidth,不设置会不显示
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孤独的冥王星

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值