前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
优快云:http://www.youkuaiyun.com
雨季o莫忧离:http://blog.youkuaiyun.com/luckkof
正文
就是很像开关的那种控件,它只有两个状态: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 |
类的概述:
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中的源码:
<SPAN style="FONT-FAMILY: System">public int getCompoundDrawablePadding() {
final Drawables dr = mDrawables;
return dr != null ? dr.mDrawablePadding : 0;
}</SPAN>
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的一个定义:
<SPAN style="FONT-FAMILY: System"><SPAN style="COLOR: #000000"><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></SPAN></SPAN>
可以在main.xml中这样定义:
<SPAN style="FONT-FAMILY: System"><SPAN style="COLOR: #000000"><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="关"
/></SPAN></SPAN>
当Switch状态切换时:
<SPAN style="FONT-FAMILY: System"><SPAN style="COLOR: #000000">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("关");
}
}
});</SPAN></SPAN>