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


其类关系图如下:
| java.lang.Object | |||||
| ↳ | android.view.View | ||||
| ↳ | android.widget.TextView | ||||
| ↳ | android.widget.Button | ||||
| ↳ | android.widget.CompoundButton | ||||
| ↳ | android.widget.Switch | ||||
类的概述:
Switch是一个可以在两种状态切换的开关控件。用户可以拖动"thumb"来回选择,也可以像选择复选框一样点击切换Switch的状态。
主要方法:
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| <nobr>int</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#getCompoundPaddingRight()" style="color: rgb(0, 102, 153); text-decoration: none; ">getCompoundPaddingRight</a></span>()</nobr>
Returns the right padding of the view, plus space for the right Drawable if any.
| ||||||||||
| <nobr><a href="http://developer.android.com/reference/java/lang/CharSequence.html" style="color: rgb(0, 102, 153); text-decoration: none; ">CharSequence</a></nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#getTextOff()" style="color: rgb(0, 102, 153); text-decoration: none; ">getTextOff</a></span>()</nobr>
Returns the text displayed when the button is not in the checked state.
| ||||||||||
| <nobr><a href="http://developer.android.com/reference/java/lang/CharSequence.html" style="color: rgb(0, 102, 153); text-decoration: none; ">CharSequence</a></nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#getTextOn()" style="color: rgb(0, 102, 153); text-decoration: none; ">getTextOn</a></span>()</nobr>
Returns the text displayed when the button is in the checked state.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#jumpDrawablesToCurrentState()" style="color: rgb(0, 102, 153); text-decoration: none; ">jumpDrawablesToCurrentState</a></span>()</nobr>
Call
Drawable.jumpToCurrentState()on all Drawable objects associated with this view.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#onMeasure(int,%20int)" style="color: rgb(0, 102, 153); text-decoration: none; ">onMeasure</a></span>(int widthMeasureSpec, int heightMeasureSpec)</nobr>
Measure the view and its content to determine the measured width and the measured height. | ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#onPopulateAccessibilityEvent(android.view.accessibility.AccessibilityEvent)" style="color: rgb(0, 102, 153); text-decoration: none; ">onPopulateAccessibilityEvent</a></span>(<a href="http://developer.android.com/reference/android/view/accessibility/AccessibilityEvent.html" style="color: rgb(0, 102, 153); text-decoration: none; ">AccessibilityEvent</a>event)</nobr>
Called from
dispatchPopulateAccessibilityEvent(AccessibilityEvent)giving a chance to this View to populate the accessibility event with its text content.
| ||||||||||
| <nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#onTouchEvent(android.view.MotionEvent)" style="color: rgb(0, 102, 153); text-decoration: none; ">onTouchEvent</a></span>(<a href="http://developer.android.com/reference/android/view/MotionEvent.html" style="color: rgb(0, 102, 153); text-decoration: none; ">MotionEvent</a>ev)</nobr>
Implement this method to handle touch screen motion events.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)" style="color: rgb(0, 102, 153); text-decoration: none; ">setChecked</a></span>(boolean checked)</nobr>
Changes the checked state of this button. | ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setSwitchTextAppearance(android.content.Context,%20int)" style="color: rgb(0, 102, 153); text-decoration: none; ">setSwitchTextAppearance</a></span>(<a href="http://developer.android.com/reference/android/content/Context.html" style="color: rgb(0, 102, 153); text-decoration: none; ">Context</a>context, int resid)</nobr>
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setSwitchTypeface(android.graphics.Typeface,%20int)" style="color: rgb(0, 102, 153); text-decoration: none; ">setSwitchTypeface</a></span>(<a href="http://developer.android.com/reference/android/graphics/Typeface.html" style="color: rgb(0, 102, 153); text-decoration: none; ">Typeface</a>tf, int style)</nobr>
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.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setSwitchTypeface(android.graphics.Typeface)" style="color: rgb(0, 102, 153); text-decoration: none; ">setSwitchTypeface</a></span>(<a href="http://developer.android.com/reference/android/graphics/Typeface.html" style="color: rgb(0, 102, 153); text-decoration: none; ">Typeface</a>tf)</nobr>
Sets the typeface in which the text should be displayed on the switch.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setTextOff(java.lang.CharSequence)" style="color: rgb(0, 102, 153); text-decoration: none; ">setTextOff</a></span>(<a href="http://developer.android.com/reference/java/lang/CharSequence.html" style="color: rgb(0, 102, 153); text-decoration: none; ">CharSequence</a>textOff)</nobr>
Sets the text displayed when the button is not in the checked state.
| ||||||||||
| <nobr>void</nobr> | <nobr><span class="sympad" style="margin-right: 2px; "><a href="http://developer.android.com/reference/android/widget/Switch.html#setTextOn(java.lang.CharSequence)" style="color: rgb(0, 102, 153); text-decoration: none; ">setTextOn</a></span>(<a href="http://developer.android.com/reference/java/lang/CharSequence.html" style="color: rgb(0, 102, 153); text-decoration: none; ">CharSequence</a>textOn)</nobr>
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(MotionEventev)实现这一方法传递触摸屏运动事件。
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("关"); } } });
基本上先简单的讲到这。
理解Android Switch控件及其使用方法
本文详细介绍了Android Switch控件的特性、类关系、主要方法和在实际开发中的应用,包括如何设置文本、状态切换和测量控件尺寸等核心功能。通过实例演示了如何在布局文件中定义Switch控件,并提供了关键属性配置示例。
959

被折叠的 条评论
为什么被折叠?



