[Android]开关控件Switch

简介: [Android]开关控件Switch

效果图:

关闭:

打开:

<Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:track="@drawable/selector"
        android:thumb="@drawable/rbzhong"
        />

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/shape1" android:state_checked="true"/>
  <item android:drawable="@drawable/shape2" android:state_checked="false"/>
</selector>

shape1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="45dp"/>
    <stroke android:width="2dp" android:color="#ff99cc"/>
    <gradient android:startColor="#ffcccc" android:centerColor="#ff6699" android:endColor="#ff3399"></gradient>
</shape>

shape2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white"/>
      <corners android:radius="45dp"/>
    <stroke android:width="2dp" android:color="#ff99cc"/>
</shape>

switch的属性

XML 属性
android:switchMinWidth 开关组件的最小宽度。
android:switchPadding 开关和标题文本之间的最小间距。
android:switchTextAppearance 文本切换拇指上显示的文本的外观样式。
android:textOff 交换机处于未选中/“关闭”状态时使用的文本。
android:textOn 交换机处于选中/“打开”状态时要使用的文本。
android:thumb 可绘制用作来回切换的“拇指”。
android:thumbTextPadding 切换拇指内文本两侧的填充量。
android:track 可绘制用作切换拇指在其中滑动的“轨道”。

事件监听

val switch1:Switch=findViewById(R.id.switch1)
        switch1.setOnCheckedChangeListener { buttonView, isChecked ->
            when(isChecked){
                true->{Toast.makeText(this,"true",Toast.LENGTH_SHORT).show()}
                false->{Toast.makeText(this,"false",Toast.LENGTH_SHORT).show()}
            }
        }
目录
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
4月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
184 0
|
18天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
3月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
23 0
|
3月前
|
Android开发
分享89个Android控件源代码总有一个是你想要的
分享89个Android控件源代码总有一个是你想要的
74 0
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习滚动控件RecyclerView
android开发,使用kotlin学习滚动控件RecyclerView
39 0
|
4月前
|
XML Java Android开发
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
72 0
|
前端开发 Java Android开发
android开关按钮
<span style="font-size:18px">刚开始接触开关样式的按钮是在IOS系统上面,它的切换以及滑动十分帅气,深入人心。</span> <p><span style="font-size:18px"><span style="white-space:pre"></span>所谓的开关按钮,就是只有2个状态:on和off,下图就是系统IOS 7上开关按钮效果。</span
1858 0
|
XML Android开发 iOS开发
android仿ios开关按钮
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> 前一段时间在做项目的时候遇到了一个问题,美工在设计的时候设计的是一个iPhone中的开关,但是都知道Android中的Switch开关和IOS中的不同,这样就需要通过动画来实现一个iPhone开关了。</p> <p s
2327 0