android4.0新控件Switch步骤解析

android4.0新控件Switch方法解析就是很像开关的那种控件,它只有两个状态:on和off:在IOS中,有个UISwitch控

android4.0新控件Switch方法解析
       就是很像开关的那种控件,它只有两个状态:on和off:在IOS中,有个UISwitch控件,其效果图,如下:

                                     android4.0新控件Switch步骤解析

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

android4.0新控件Switch步骤解析   android4.0新控件Switch步骤解析          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 MethodsintgetCompoundPaddingRight()Returns the right padding of the view, plus space for the right Drawable if any.CharSequencegetTextOff()Returns the text displayed when the button is not in the checked state.CharSequencegetTextOn()Returns the text displayed when the button is in the checked state.voidjumpDrawablesToCurrentState()Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

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

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

Changes the checked state of this button.

voidsetSwitchTextAppearance(Context context, int resid)Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.voidsetSwitchTypeface(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.voidsetSwitchTypeface(Typeface tf)Sets the typeface in which the text should be displayed on the switch.voidsetTextOff(CharSequence textOff)Sets the text displayed when the button is not in the checked state.voidsetTextOn(CharSequence textOn)Sets the text displayed when the button is in the checked state.

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

在TextView中的源码:


基本上先简单的讲到这。