android4.0新控件Switch方法解析

2023-05-16

        就是很像开关的那种控件,它只有两个状态:on和off:在IOS中,有个UISwitch控件,其效果图,如下:

                                     

在android4.0里面,添加了一个和这个类似的控件:Switch,很形象,开关。效果图如下:

             


其类关系图如下:

java.lang.Object
   ↳android.view.View
    ↳android.widget.TextView
     ↳android.widget.Button
      ↳android.widget.CompoundButton
       ↳android.widget.Switch
父类:compoundButton。

类的概述:

    Switch是一个可以在两种状态切换的开关控件。用户可以拖动"thumb"来回选择,也可以像选择复选框一样点击切换Switch的状态。

主要方法:

Public Methods
int getCompoundPaddingRight()
Returns the right padding of the view, plus space for the right Drawable if any.
CharSequence getTextOff()
Returns the text displayed when the button is not in the checked state.
CharSequence getTextOn()
Returns the text displayed when the button is in the checked state.
void jumpDrawablesToCurrentState()
Call  Drawable.jumpToCurrentState() on all Drawable objects associated with this view.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onPopulateAccessibilityEvent( AccessibilityEvent event)
Called from  dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content.
boolean onTouchEvent( MotionEvent ev)
Implement this method to handle touch screen motion events.
void setChecked(boolean checked)

Changes the checked state of this button.

void setSwitchTextAppearance( Context context, int resid)
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
void setSwitchTypeface( Typeface tf, int style)
Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.
void setSwitchTypeface( Typeface tf)
Sets the typeface in which the text should be displayed on the switch.
void setTextOff( CharSequence textOff)
Sets the text displayed when the button is not in the checked state.
void setTextOn( CharSequence textOn)
Sets the text displayed when the button is in the checked state.

getCompoundPaddingRight():没弄清楚什么意思。

在TextView中的源码:

public int getCompoundDrawablePadding() {
        final Drawables dr = mDrawables;
        return dr != null ? dr.mDrawablePadding : 0;
    }

jumpDrawableToCurrentState():在与Switch相关的Drawable操作时调用 Drawable.jumpToCurrentState()这个方法。

     getTextOff()、getTextOn()、 setTextOff()、setTextOn()这四个方法比较简单,就是设定和获取非选中和选中状态下的文本值。

     onMeasure():测量控件宽高,供绘图时使用。

     onTouchEvent(MotionEvent ev)实现这一方法传递触摸屏运动事件。

setChecked()设置Switch的状态(选中,非选中)

setSwitchTextAppearance()设置字体大小

setSwitchTextTypefaces设置字体格式


看看google官方在/frameworks/base/core/res/res/values/styles.xml的一个定义:

<style name="Widget.Holo.CompoundButton.Switch">
        <item name="android:track">@android:drawable/switch_track_holo_dark</item>
        <item name="android:thumb">@android:drawable/switch_inner_holo_dark</item>
        <item name="android:switchTextAppearance">@android:style/TextAppearance.Holo.Widget.Switch</item>
        <item name="android:textOn">@android:string/capital_on</item>
        <item name="android:textOff">@android:string/capital_off</item>
        <item name="android:thumbTextPadding">12dip</item>
        <item name="android:switchMinWidth">96dip</item>
        <item name="android:switchPadding">16dip</item>
    </style>

可以在main.xml中这样定义:

<Switch 
        android:id="@+id/demo_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:textOn="开"
        android:textOff="关"
        />

当Switch状态切换时:

mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked) {
					//选中时 do some thing 
					statusText.setText("开");
				} else {
					//非选中时 do some thing 
					statusText.setText("关");
				}
				
			}
		});

基本上先简单的讲到这。



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android4.0新控件Switch方法解析 的相关文章

  • android4.0默认界面旋转180

    http my eoe cn 876641 archive 22638 html android4 0默认启动画面和正常显示旋转了180度 xff0c 即为倒立的 原来是屏输出为倒的 xff0c 查找得知可以做旋转 xff1a 步骤 xff
  • 菜鸟Android4.0 Settings分析(一)

    先声明 xff1a 本人工作半年 xff0c 是真的菜鸟 xff0c 之前有做过2 3的Launcher xff0c 没有记录下来 xff0c 感觉挺可惜的 xff0c 现在老大叫我搞Setting xff0c 我觉得是得写得东西 xff0
  • switch 无法启动软件,请在HOME菜单中再试一次

    本人的是11 0 1 0 18 1 xff0c 刚升级完成后所有游戏都提示 xff1a 无法启动软件 xff0c 请在HOME菜单中再试一次 xff1b 然后搜索一番之后亲测有效 1将如下资源下载到本地 xff0c 2解压后放到对应的文件夹
  • python中switch语句用法

    python中是没用switch语句的 xff0c 这应该是体现python大道至简的思想 xff0c python中一般多用字典来代替switch来实现 coding utf 8 from future import division d
  • 能在 Switch 中使用 String 吗?

    答 xff1a 从 Java 7 开始 xff0c 我们可以在 switch case 中使用字符串 xff0c 但这仅仅是一个语法糖 内部实现在 switch 中使用字符串的 hashCode 补充 xff1a 在JDK7以前 xff0c
  • ‘break‘ not in the ‘loop‘ or ‘switch‘ context

    使用phpexcel导出表格 xff0c 切换到php7 0报错 错误地址 xff1a FILE ThinkPHP Library Vendor phpexcel PHPExcel Calculation Functions php LIN
  • amiibo数据_Switch自制Amiibo解锁集齐所有卡贴成就

    任天堂在2014年6月E3展上发布了Amiibo xff0c 即内置近场通信NFC芯片的手办 Amiibo 英文 xff1a Amiibo xff0c 日文 xff1a xff0c 用在Wii U 任天堂3DS和任天堂Switch上 xff
  • React路由里exact和switch的区别

    React路由里exact和switch的区别 exact属性 react的路由匹配默认是模糊的 xff0c 包容的 xff0c 如果想使用严格匹配 xff0c 那么 xff0c 把Route组件的exact属性设置为true span c
  • 【5G核心网】free5GC Path Switch Request源码分析

    Path Switch Request 过程的目的是请求将 NG U 传输承载的下行链路终结点切换到新的终结点 Figure 8 4 4 2 1 Path switch request successful operation NG RAN
  • switch case语句的用法

    switch case语句的用法 我们首先来看一下switch case 语句的一般形式是怎样的 span class token keyword switch span xff08 表达式 xff09 xff5b span class t
  • switch case的用法

    switch case的用法如下 1 Switch用在编程中 xff0c 如C语言中它经常跟Case一起使用 xff0c 是一个判断选择代码 其功能就是控制流程流转的 直线翻译 xff1a switch语句 xff0c 即 切换 语句 xf
  • switch 中 break 和 continue 的区别

    1 break 用来退出 switch xff0c continue 本身是不能用在 switch 里的 xff0c 他必须结合循环来用 xff0c 表示跳过本次循环 2 switch 的 case 语句最后如果没有加 break cont
  • PPPoE报文格式及交互详解

    简介 PPPoE报文的格式就是在以太网帧中携带PPP报文 如图所示 各个字段解释如下 Destination address 一个以太网单播目的地址或者以太网广播地址 0xffffffff 对于Discovery数据包来说 该域的值是单播或
  • 多路开关模式的switch语句

    在实例10中 将break语句去掉之后 会将符合检验条件后的所有语句都输出 利用这个特点 可以设计多路开关模式的switch语句 例如 在平年一年12个月 1 3 5 7 8 10 12月是31天 4 6 9 11月是30天 2月是28天
  • 用switch语句实现简单的取款机程序

    大家好 今天给大家分享一个简单的取款机程序 相关函数 switch语句 分支语句 case语句 分支条件 break语句 结束循环 default语句 其他分支 演示效果 试试存款 存款成功 再试试取款 取款也成功 代码部分 源文件名 ma
  • Java分支语句

    一 键盘输入数据 1 导入java util Scanner包 语句import java util Scanner 这句话要放在class定义的上面 2 创建对象 语句 Scanner sc new Scanner System in 3
  • uniapp switch按钮的使用

    switch使用官方文档 https uniapp dcloud io component switch 想要改变switch按钮的大小
  • Android开发中关于“invalid symbol”的错误

    Android开发中 有时会提示关于 invalid symbol 这个的错误 比如 error invalid symbol switch 代码如下
  • 在switch语句中使用字符串以及实现原理

    对于Java语言来说 在Java 7之前 switch语句中的条件表达式的类型只能是与整数类型兼容的类型 包括基本类型char byte short和int 与这些基本类型对应的封装类Character Byte Short和Integer
  • Switch / Xbox / PS / PSV / PSP 游戏指南

    1 Switch Xbox PS PSV PSP 对比分析 table 1 PSV2000 1000 比较分析 PSV2000 相对 PSV1000 具体变更点 薄了20 轻了15 屏幕改为液晶屏 PS键 开始键和选择键变成球形更方便按 内

随机推荐

  • 高级设置/FTP IPv4地址和域限制(三)

    xff13 詳細設定 xff0f FTP IPv4 制限 xff13 高级设置 xff0f FTP IPv4地址和域限制 xff11 操作 項目 FTP 管理 詳細設定 xff12 高级设置 初期設定値如下 xff0a 物理路径 E act
  • Linux简易DDNS配置教程

    Linux简易DDNS配置教程 DDNS与其在Linux系统上的应用 1 1 DDNS是什么 xff0c 其作用是什么 DDNS xff08 Dynamic Domain Name System xff0c 动态域名系统 xff09 是一种
  • 机器学习毕业设计 大数据股票数据量化分析与预测系统 - python

    文章目录 0 前言1 课题背景2 实现效果UI界面设计web预测界面RSRS选股界面 3 软件架构4 工具介绍Flask框架MySQL数据库LSTM 0 前言 x1f525 这两年开始毕业设计和毕业答辩的要求和难度不断提升 xff0c 传统
  • HTML Parsing Error: Unable to modify the parent co

    HTML Parsing Error Unable to modify the parent container element before the child element is closed KB927917 主要是因为页面没有加载
  • 解决PowerShell无法使用conda的问题

    目录 1 问题描述2 解决办法2 1 将Anaconda添加至系统环境变量2 2 初始化PowerShell2 3 设置ExecutionPolicy的值 3 避免PowerShell默认激活base环境 1 问题描述 由于新版本的Anac
  • [RK3288][Android6.0] 调试笔记 --- 录音apk无权限录音问题

    Platform Rockchip OS Android 6 0 Kernel 3 10 92 现象 xff1a 写了个apk测试录音 xff0c 提示 xff1a 01 22 00 59 40 795 215 948 W ServiceM
  • 【Linux】Ubuntu 使用指南

    content 1 换清华源2 更新三步走3 1 换清华源 备份 Ubuntu 的软件源配置文件 etc apt sources list span class token function sudo span span class tok
  • ubuntu下解决不能识别外部设备的方法

    首先确认手机连接上电脑 xff0c lsusb查看下设备记录 matthew 64 matthew 1230 laptop lsusb Bus 007 Device 009 ID 18d1 4e12 Bus 007 Device 001 I
  • android json解析及简单例子

    JSON的定义 xff1a 一种轻量级的数据交换格式 xff0c 具有良好的可读和便于快速编写的特性 业内主流技术为其提供了完整的解决方案 xff08 有点类似于正则表达式 xff0c 获得了当今大部分语言的支持 xff09 xff0c 从
  • Ubuntu 16.04 如何安装 Python 3.6

    在Ubuntu 16 04版本中 xff0c 系统默认安装 了python 2 7和3 5版本 xff0c 此次安装的是新版本Python 3 6 13 由于系统已经默认安装了Python xff0c 所以相关的依赖文件已经安装妥善 xff
  • ubnutu桌面环境Gnome 配置tweak tool时看不到extension插件选项

    问题 xff1a tweak tool中没用extension选项 xff0c 这是因为没有开启gnome xff0c 解决方法是注销当前用户 然后在登录窗口的右上角 xff0c 选择gnome xff0c 如下图所示 然后在弹出的窗口中选
  • C# 内存与性能优化

    C 内存与性能优化 https www jianshu com p d56f79d83ebd 前两周分享了资源配置与资源管理 xff0c 今天分享一种特殊的资源脚本数据 在Unity项目中 xff0c 我们通常使用C 编写脚本 xff0c
  • 转发——从搭建小系统到架构分布式

    从搭建小系统到架构分布式 从搭建小系统到架构分布式 SpringBoot是目前Spring技术体系中炙手可热的框架之一 既可用于构建业务复杂的企业应用系统 xff0c 也可以开发高性能和高吞吐量的互联网应用 Spring Boot 框架降低
  • 2018-8-30华为机试第三题

    一个很明显的递归问题 package cn csu ksh import java util ArrayList import java util List import java util Scanner public class Mai
  • Android学习之Sensor

    转自http javatest blog 163 com blog static 20865106420126216118757 只需要五步 xff0c 你就能搞定Sensor 让你的程序变的更酷 java view plain copy
  • 虚拟现实技术vr可以用来干什么?虚拟现实技术vr有什么特征

    科技行业的不断蓬勃发展 xff0c 每天会出现一些新的科技产品 xff0c 例如现在很火的虚拟现实技术vr xff0c 虚拟现实技术用的领域很多 xff0c 就拿游戏行业来说 xff0c 玩家可以通过vr眼镜 vr手柄等体验vr游戏 xff
  • Ubuntu18.04安装Qt5.14.2

    1 去官网 xff08 https download qt io archive qt xff09 下载对应的 run版本 这里是5 14 2 2 进入下载后的路径 xff0c 先赋予权限 xff0c 再安装 span class toke
  • Python归并排序

    归并排序 数据科学家每天都在处理算法 然而 xff0c 数据科学学科作为一个整体已经发展成为一个不涉及复杂算法实现的角色 尽管如此 xff0c 从业者仍然可以从建立对算法的理解和知识库中受益 在本文中 xff0c 对排序算法归并排序进行了介
  • Android ADB 源码分析总结

    Android之ADB总结 本文内容如下 xff1a 1 makefile分析及总结 2 adb框架介绍 3 adbd源码分析 3 1 adbd初始化流程分析 3 2 adb shell流程分析 3 3 adb root流程分析 4 adb
  • android4.0新控件Switch方法解析

    就是很像开关的那种控件 xff0c 它只有两个状态 xff1a on和off xff1a 在IOS中 xff0c 有个UISwitch控件 xff0c 其效果图 xff0c 如下 xff1a 在android4 0里面 xff0c 添加了一