android4.0新控件Switch方法解析

本文详细介绍了 Android 4.0 中新增的 Switch 控件。Switch 控件是一种可以在两种状态之间切换的开关,用户可以通过拖动滑块或者点击来改变状态。文章提供了 Switch 控件的主要方法介绍,并展示了如何在 XML 布局文件中定义 Switch 控件及其监听器。

原文地址:http://blog.youkuaiyun.com/lilu_leo/article/details/7661459




就是很像开关的那种控件,它只有两个状态: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中的源码:

[java]  view plain copy
  1. <span style="font-family:System;">public int getCompoundDrawablePadding() {  
  2.         final Drawables dr = mDrawables;  
  3.         return dr != null ? dr.mDrawablePadding : 0;  
  4.     }</span>  

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的一个定义:

[java]  view plain copy
  1. <span style="font-family:System;"><span style="color:#000000;"><style name="Widget.Holo.CompoundButton.Switch">  
  2.         <item name="android:track">@android:drawable/switch_track_holo_dark</item>  
  3.         <item name="android:thumb">@android:drawable/switch_inner_holo_dark</item>  
  4.         <item name="android:switchTextAppearance">@android:style/TextAppearance.Holo.Widget.Switch</item>  
  5.         <item name="android:textOn">@android:string/capital_on</item>  
  6.         <item name="android:textOff">@android:string/capital_off</item>  
  7.         <item name="android:thumbTextPadding">12dip</item>  
  8.         <item name="android:switchMinWidth">96dip</item>  
  9.         <item name="android:switchPadding">16dip</item>  
  10.     </style></span></span>  

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

[java]  view plain copy
  1. <span style="font-family:System;"><span style="color:#000000;"><Switch   
  2.         android:id="@+id/demo_switch"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_below="@+id/textView"  
  6.         android:textOn="开"  
  7.         android:textOff="关"  
  8.         /></span></span>  

当Switch状态切换时:

[java]  view plain copy
  1. <span style="font-family:System;"><span style="color:#000000;">mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  2.               
  3.             @Override  
  4.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  5.                 if(isChecked) {  
  6.                     //选中时 do some thing   
  7.                     statusText.setText("开");  
  8.                 } else {  
  9.                     //非选中时 do some thing   
  10.                     statusText.setText("关");  
  11.                 }  
  12.                   
  13.             }  
  14.         });</span></span>  

基本上先简单的讲到这。
计及源荷不确定性的综合能源生产单元运行调度与容量配置优化研究(Matlab代码实现)内容概要:本文围绕“计及源荷不确定性的综合能源生产单元运行调度与容量配置优化”展开研究,利用Matlab代码实现相关模型的构建与仿真。研究重点在于综合能源系统中多能耦合特性以及风、光等可再生能源出力和负荷需求的不确定性,通过鲁棒优化、场景生成(如Copula方法)、两阶段优化等手段,实现对能源生产单元的运行调度与容量配置的协同优化,旨在提高系统经济性、可靠性和可再生能源消纳能力。文中提及多种优化算法(如BFO、CPO、PSO等)在调度与预测中的应用,并强调了模型在实际能源系统规划与运行中的参考价值。; 适合人群:具备一定电力系统、能源系统或优化理论基础的研究生、科研人员及工程技术人员,熟悉Matlab编程和基本优化工具(如Yalmip)。; 使用场景及目标:①用于学习和复现综合能源系统中考虑不确定性的优化调度与容量配置方法;②为含高比例可再生能源的微电网、区域能源系统规划设计提供模型参考和技术支持;③开展学术研究,如撰写论文、课题申报时的技术方案借鉴。; 阅读建议:建议结合文中提到的Matlab代码和网盘资料,先理解基础模型(如功率平衡、设备模型),再逐步深入不确定性建模与优化求解过程,注意区分鲁棒优化、随机优化与分布鲁棒优化的适用场景,并尝试复现关键案例以加深理解。
内容概要:本文系统分析了DesignData(设计数据)的存储结构,围绕其形态多元化、版本关联性强、读写特性差异化等核心特性,提出了灵活性、版本化、高效性、一致性和可扩展性五大设计原则。文章深入剖析了三类主流存储方案:关系型数据库适用于结构化元信息存储,具备强一致性与高效查询能力;文档型数据库适配半结构化数据,支持动态字段扩展与嵌套结构;对象存储结合元数据索引则有效应对非结构化大文件的存储需求,具备高扩展性与低成本优势。同时,文章从版本管理、性能优化和数据安全三个关键维度提出设计要点,建议采用全量与增量结合的版本策略、索引与缓存优化性能、并通过权限控制、MD5校验和备份机制保障数据安全。最后提出按数据形态分层存储的核心结论,并针对不同规模团队给出实践建议。; 适合人群:从事工业设计、UI/UX设计、工程设计等领域数字化系统开发的技术人员,以及负责设计数据管理系统架构设计的中高级工程师和系统架构师。; 使用场景及目标:①为设计数据管理系统选型提供依据,合理选择或组合使用关系型数据库、文档型数据库与对象存储;②构建支持版本追溯、高性能访问、安全可控的DesignData存储体系;③解决多用户协作、大文件存储、历史版本管理等实际业务挑战。; 阅读建议:此资源以实际应用场景为导向,结合具体数据库类型和表结构设计进行讲解,建议读者结合自身业务数据特征,对比分析不同存储方案的适用边界,并在系统设计中综合考虑成本、性能与可维护性之间的平衡。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值