序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下。

1.Switch

  1. <Switch
  2. android:id="@+id/bt"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:textOn="开启"
  6. android:layout_marginLeft="20dp"
  7. android:textOff="关闭"
  8. android:showText="true"
  9. android:thumb="@drawable/shape_thum"
  10. android:track="@drawable/select_bg_switch"
  11. />

这里layout_width:这能设置整个布局的宽度,不能设置具体的Switch的大小,需要使用switchMinWidth属性来设置。

thumb:文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。

track:设置开关的背景图片,类似于button的background。

textoff、texton:设置开关时的文字显示。

2.ToggleButton

  1. <ToggleButton
  2. android:layout_width="80dp"
  3. android:layout_height="20dp"
  4. android:id="@+id/toggle"
  5. android:layout_centerHorizontal="true"
  6. android:background="@drawable/shape_track_on"
  7. android:textOff="off"
  8. android:textOn="on"
  9. android:layout_marginLeft="20dp"
  10.  
  11. />

3.效果图

1.布局

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center"
  10. android:orientation="horizontal"
  11. android:layout_marginTop="30dp"
  12. >
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="开关"
  17. />
  18. <ToggleButton
  19. android:layout_width="80dp"
  20. android:layout_height="20dp"
  21. android:id="@+id/toggle"
  22. android:layout_centerHorizontal="true"
  23. android:background="@drawable/shape_track_on"
  24. android:textOff="off"
  25. android:textOn="on"
  26. android:layout_marginLeft="20dp"
  27.  
  28. />
  29. </LinearLayout>
  30.  
  31. <LinearLayout
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:orientation="horizontal"
  35. android:layout_centerInParent="true"
  36. >
  37. <TextView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="开启状态"
  41. />
  42.  
  43. <Switch
  44. android:id="@+id/bt"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:textOn="开启"
  48. android:layout_marginLeft="20dp"
  49. android:textOff="关闭"
  50. android:showText="true"
  51. android:thumb="@drawable/shape_thum"
  52. android:track="@drawable/select_bg_switch"
  53. />
  54.  
  55. </LinearLayout>
  56.  
  57. </RelativeLayout>

shape_thum.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
  3. <solid android:color="#0f0"/>
  4. <size android:height="30dp" android:width="30dp"/>
  5. </shape>

shape_track_off.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  3. <corners android:radius="30dp"/>
  4. <solid android:color="@android:color/darker_gray"/>
  5. </shape>

shape_track_on,xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  3. <corners android:radius="30dp"/>
  4. <solid android:color="@android:color/holo_red_light"/>
  5. </shape>

select_bg_switch.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_checked="true" android:drawable="@drawable/shape_track_on"/>
  4. <item android:state_checked="false" android:drawable="@drawable/shape_track_off"/>
  5.  
  6. </selector>

Mainactivity

  1. public class MainActivity extends AppCompatActivity {
  2. private Switch aSwitch;
  3. private ToggleButton toggle;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. aSwitch=findViewById(R.id.bt);
  9. toggle=findViewById(R.id.toggle);
  10.  
  11. //状态改变监听
  12. aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  13. @Override
  14. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  15. if(isChecked){
  16. Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();
  17. }else{
  18. Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();
  19. }
  20. }
  21. });
  22.  
  23. //状态改变监听
  24. toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  25. @Override
  26. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  27. if(isChecked){
  28. Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();
  29. }else{
  30. Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();
  31. }
  32. }
  33. });
  34. }
  35. }

android开关控件Switch和ToggleButton的更多相关文章

  1. android自己定义开关控件

    近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图:   waterma ...

  2. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  3. Android之控件使用

    Android系统为我们提供了大量的控件,例如:开关控件.单选按钮.多选按钮.单选菜单等等,那么这些控件如何使用呢?本篇我将带领大家一道学习一下如何使用这些控件.所谓无图无真相,先让大家看一下效果图: ...

  4. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  5. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  6. UISwitch 开关控件

    UISwitch iOS中的开关控件,只有两种状态,打开或关闭. aSwitch.tintColor = [UIColor redColor]; //关闭状态下的渲染颜色 aSwitch.onTint ...

  7. Android给控件添加触摸回调

    Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...

  8. weui-switch开关控件,表单提交后如何取值

    最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...

  9. [Xcode 实际操作]四、常用控件-(6)UISwitch开关控件的使用

    目录:[Swift]Xcode实际操作 本文将演示开关控件的基本用法. 开关控件有两个互斥的选项,它是用来打开或关闭选项的控件. 在项目导航区,打开视图控制器的代码文件[ViewController. ...

随机推荐

  1. 在ERP软件行业项目应该怎么做?

    本人进入软件行业已经有5个年头了,主导实施和参与过的大大小小的项目有20余个.其中有些项目做的非常好,提升了客户企业的管理水平,也规范了企业的业务操作水平,优化了企业的流程,客户非常认同我们实施的价值 ...

  2. java代码如何替换字符

    package com.aa; public class Ss { public static void main(String[] args) { String a = "ABCD&quo ...

  3. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. DB2的安装

    jiangxin@db01:~$ su – root #切换到root用户 密码: root@db01:~# uname -a #查看内核和操作系统信息 Linux db01 4.4.0-66-gen ...

  5. ZedGraph类库之基本教程篇

      第一部分:基本教程篇                 ZedGraphDemo中一共有9个基本教程的例子.其中大部分都类似,我会讲解其中一些比较典型的例子.把ZedGraph类库的使用逐步展现给大 ...

  6. python's fifteenth day for me 递归函数

    递归... def age(n): if n == 1: return 18 else: return age(n-1)+2 # 反复调用函数age() print(age(4)) l = [1,3, ...

  7. 对于现代开发来说,JavaScript就是一种垃圾语言(转)

    伯乐在线导读:昨天 Reddit/Programming 频道的头条热帖是一篇来自 julik live 博主的技术吐槽文,最初的英文标题是"For modern development J ...

  8. linux中stdout,stdin,stderr意义

    stdout, stdin, stderr的中文名字分别是标准输出,标准输入和标准错误. 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据流,也就是题目中所提到的这三个.那么 ...

  9. linux下搭建android NDK开发环境

      1)下载android-ndk-r4 下载地址 http://www.ideasandroid.com/android/sdk/android-ndk-r4-linux-x86.zip http: ...

  10. (转)Mac下MySql安装经历(含安装错误排查、卸载多种折腾)

    在安装mysql的时候,活活折腾我两天.结果终于被我折腾成功了……一开始我就放了个错误:我下了32位版本的mysql:mysql-5.5.8-osx10.6-x86.dmg 须知在mac下装的是64位 ...