ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似。CompoundButton有两个状态,分别是checked和not checked。

ToggleButton的属性:

Switch组件的属性:

android:thumb是选中时的背景

例:开关按钮控制布局方向

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ToggleButtonandroid:id="@+id/toggleButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ToggleButton"android:textOff="横向排列"android:textOn="纵向排列" />
<!-- android:thumb="@drawable/check" 使用自定义的drawable对象绘制开关按钮 --><Switchandroid:id="@+id/switch1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Switch"android:textOff="横向排列"android:textOn="纵向排列"android:thumb="@drawable/check" />
<LinearLayout android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3" />
</LinearLayout>
</LinearLayout>

MainActivity.java

 ToggleButton toggle;Switch switcher;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);toggle=(ToggleButton) findViewById(R.id.toggleButton1);switcher=(Switch) findViewById(R.id.switch1);final LinearLayout test=(LinearLayout) findViewById(R.id.root);//ToggleButton和Switch的监听接口和复选框CheckButton的一样CompoundButton.OnCheckedChangeListener listener=new  CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton button, boolean checkedId) {// TODO Auto-generated method stubif(checkedId){//1表示垂直布局,0表示水平布局test.setOrientation(1);}else{test.setOrientation(0);}}        };toggle.setOnCheckedChangeListener(listener);switcher.setOnCheckedChangeListener(listener);}

以后要学会自己定制跟家美观的组件

推荐几个好的博客:

http://blog.csdn.net/billpig/article/details/6634481

http://blog.csdn.net/luoweifu/article/details/11752035

UI组件之TextView及其子类(三)ToggleButton和Switch相关推荐

  1. UI组件之TextView及其子类(二)RadioButton和CheckBox

    单选按钮(RadioButton)和复选框(CheckBox),状态开关按钮(ToggleButton),开关(Switch)都是普通的UI组件,都继承了Button类,因此都可以用Button的各种 ...

  2. UI组件之TextView及其子类(五)计时器Chronometer

    Chronometer直接继承了TextView组件,它会显示一段文本,显示从某个其实时间开始,一共过了多长时间.我们看Chronometer的源码: 可以看出计时器是直接继承了TextView,当然 ...

  3. UI组件之TextView及其子类(四)AnalogClock,DigitalClock

    DigitalClock本身就继承了TextView,也就是说他本身就是文本框,只是显示的内容总是当前时间,DigitalClock设置的android:text属性没什么作用. AnalogCloc ...

  4. UI组件之TextView及其子类(一)TextView和EditText

    先来整理一下TexView,EditView的用法. Textview是最基本的组件,直接继承了View,也是众多组件的父类,所以了解她的属性会对学习其他组件很有帮助. TextView的属性: an ...

  5. UI组件之TextView及其子类

    学习完了需要经常总结,可能总结的不够全面,但是也是自己学习的进步.总结了如下图的关系,android那么多组件,清楚组件之间的关系感觉也是很重要的!下图中的每个组件的代码使用例程自己都敲过了,印象深刻 ...

  6. UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView

    AdapterView的内容一般是包含多项相同格式资源的列表,常用的有5种AdapterView的子类: (1)ListView:简单的列表 (2)Spinner:下拉列表,给用户提供选择 (3)Ga ...

  7. UI组件之AdapterView及其子类(三)Spinner控件详解

    Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单或者一个dialog对话框,从该 ...

  8. UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用

    ExpandableListView是ListView的子类,他在ListView上进行了扩展,它把列表项分成了几组,每组里包含了多个列表项 ExpandableListView的列表项是由Expan ...

  9. UI组件之AdapterView及其子类关系,Adapter接口及其实现类关系

    AdapterView本身是一个抽象基类,它派生的的子类在用法上十分相似.AdapterView直接派生的三个子类:AbsListView,AbsSpinner,AdapterViewAnimator ...

最新文章

  1. mysql 多个unique key_[MySQL]MySQL 中通过使用UNIQUE KEY 来控制字段值不重复的问题.
  2. 代理 block 通知传值
  3. vuex数据管理-数据适配
  4. 小米面试题:合并二叉树
  5. python 链表中倒数第k个节点
  6. 《系统集成项目管理工程师》必背100个知识点-45质量管理
  7. 读书笔记_CLR.via.c#第十六章_数组
  8. 虹软java接摄像头_虹软人脸识别SDK(java+linux/window) 初试
  9. HDU 2202 计算几何
  10. connectionstring mysql_Entity Framework 6 自定义连接字符串ConnectionString连接MySQL
  11. php gzip乱码,php file_get_contents抓取Gzip网页乱码的三种解决方法
  12. 玩转SpringSession,重要知识点全面剖析(续篇)
  13. 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题 1
  14. Power BI中的AI语义分析应用:《辛普森一家》
  15. 博文视点 on Twitter
  16. Linux文件系统的正确挂载方式
  17. 《utils》yaml,yml格式化
  18. 2018 ps常用字体打包下载
  19. secureCRT免密码登陆Linux
  20. 如何制作微信答题小程序(微信答题考试小程序开发制作功能介绍)

热门文章

  1. 【web项目】—the type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from ....
  2. javaWeb_JSP 动态指令 forward 的程序
  3. 设计模式--观察者(Observer)模式
  4. 自动化测试之鼠标悬浮操作、双击、鼠标拖拽
  5. python虚拟环境的使用
  6. HTML5中各种标签总结(body标签)
  7. UnboundLocalError: local variable ‘XXX‘ referenced before assignment解决办法
  8. 03_Introduction_to_AMBA_AXI
  9. optee中User TA的加载/验签和运行
  10. 2021-07-09