Android7.0中文文档(API) -- Switch

来源:互联网 发布:php计算器代码 编辑:程序博客网 时间:2024/06/05 00:11

完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Switch.html

Switch

public class Switch
extendsCompoundButton

java.lang.Object   ↳android.view.View    ↳android.widget.TextView     ↳android.widget.Button      ↳android.widget.CompoundButton       ↳android.widget.Switch

A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox. Thetext property controls the text displayed in the label for the switch, whereas theoff andon text controls the text on the thumb. Similarly, thetextAppearance and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance and the related setSwitchTypeface() methods control that of the thumb.
Switch 是一个可以选择两种选项的双态控件。用户可以拖动“滑块(thumb)”来回选择所选的选项,或简单地点击切换,把它当做一个单选框(checkbox)。这个text属性控制着在switch中显示的文本, 而设置off (文本)和 设置on( 文本)控制在滑块(thumb)上显示的文本。同样,在textAppearance及相关setTypeface() 方法里控制着字体和文本样式的标签,而switchTextAppearance及相关setSwitchTypeface()控制着滑块(thumb)。

SwitchCompat is a version of the Switch widget which runs on devices back to API 7.
SwitchCompat 这个版本的Switch控件运行在 API 7 的设备中。

See the Toggle Buttons guide.
请参考Toggle Buttons。

摘要


XML属性

android:showText是否绘制on/off文本。 android:splitTrack是否拆分轨道(track), 为滑块(thumb)的drawable留下一个缺口。 android:switchMinWidthswitch控件的最小宽度

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". 
尺寸,带单位的一个浮点数,例如:14.5sp。 

android:switchPaddingswitch控件与说明文本之间的最小间距

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". 
尺寸,带单位的一个浮点数,例如:14.5sp。 

android:switchTextAppearanceswitch控件滑块上显示的文本的TextAppearance样式。 android:textOff当开关处于未选中/“关闭”状态时使用的文本。 android:textOn当开关处于选中/“开启”状态时使用的文本。 android:textStyle文本样式(粗体,斜体,粗斜体) android:thumb来回切换滑块(thumb)的图片资源(Drawable) android:thumbTextPaddingswitch控件的滑块内的文本两侧的内边距(padding)。 android:thumbTint用于滑块的tint。 android:thumbTintMode混合模式,用于滑块着色。 android:trackswitch控件的滑轨drawable,滑块在这个滑轨中。 android:trackTint用于滑轨的tint。 android:trackTintMode混合模式,用于滑轨着色。 android:typeface文本字体(标准字体, sans字体, serif字体, monospace字体) 

继承XML属性

From class android.widget.CompoundButton
From class android.widget.TextView
From class android.view.View

继承常量

From class android.widget.TextView
From class android.view.View

继承字段

From class android.view.View

Public构造方法

Switch(Context context)

Construct a new Switch with default styling.
构造一个默认样式的新Switch。

Switch(Context context, AttributeSet attrs)

Construct a new Switch with default styling, overriding specific style attributes as requested.
构造一个默认样式的新Switch,其请求指定的样式属性覆盖。

Switch(Context context, AttributeSet attrs, int defStyleAttr)

Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.
构造一个由指定的主题属性决定的默认样式的新Switch,其请求指定的样式属性覆盖。

Switch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Construct a new Switch with a default style determined by the given theme attribute or style resource, overriding specific style attributes as requested.
构造一个由指定的主题属性或样式资源决定的默认样式的新Switch,其请求指定的样式属性覆盖。

Public方法

voiddraw(Canvas c)

Manually render this view (and all of its children) to the given Canvas.
手动将此视图 (及其所有子项) 渲染到指定的Canvas。

voiddrawableHotspotChanged(float x, float y)

This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.
每当视图热点发生改变时,调用此方法,并需要传播到由视图管理的drawable或子视图。

CharSequencegetAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.
返回此对象的类名,用于辅助功能目的。

intgetCompoundPaddingLeft()

Returns the left padding of the view, plus space for the left Drawable if any.
返回视图左内边距(padding)的大小,如果有左边Drawable的话,则加上它的空间。

intgetCompoundPaddingRight()

Returns the right padding of the view, plus space for the right Drawable if any.
返回视图右内边距(padding)的大小,如果有右边Drawable的话,则加上它的空间。

booleangetShowText()booleangetSplitTrack()

Returns whether the track should be split by the thumb.
返回轨道是否应该被滑块分割。

intgetSwitchMinWidth()

Get the minimum width of the switch in pixels.
获取视图的最小宽度(以像素为单位)。

intgetSwitchPadding()

Get the amount of horizontal padding between the switch and the associated text.
返回开关和关联文字之间的水平距离。

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.
返回按钮选中时显示的文本。

DrawablegetThumbDrawable()

Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
获取switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

intgetThumbTextPadding()

Get the horizontal padding around the text drawn on the switch itself.
获取switch文本的水平内边距(padding)。

ColorStateListgetThumbTintList()PorterDuff.ModegetThumbTintMode()DrawablegetTrackDrawable()

Get the drawable used for the track that the switch slides within.
获取轨道的drawable,其用于switch内部。

ColorStateListgetTrackTintList()PorterDuff.ModegetTrackTintMode()voidjumpDrawablesToCurrentState()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.
在与此视图关联的所有drawable对象上,调用Drawable.jumpToCurrentState()

voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.
测量视图,它的内容决定测量得到宽度和高度。

voidonProvideAutoFillStructure(ViewStructure structure, int flags)

Called when assist structure is being retrieved from a view as part of an auto-fill request.
当从一个视图中获取辅助结构时调用, 作为auto-fill请求的一部分。

voidonProvideStructure(ViewStructure structure)

Called when assist structure is being retrieved from a view as part of Activity.onProvideAssistData.
当从一个视图中获取辅助结构时调用, 作为Activity.onProvideAssistData的一部分。

booleanonTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.
实现此方法,以处理触摸屏的MotionEvent。

voidsetChecked(boolean checked)

Changes the checked state of this button.
改变按钮的选中状态。

voidsetShowText(boolean showText)

Sets whether the on/off text should be displayed.
设置是否显示打开/关闭文本。

voidsetSplitTrack(boolean splitTrack)

Specifies whether the track should be split by the thumb.
设置轨道是否应该被滑块分割。

voidsetSwitchMinWidth(int pixels)

Set the minimum width of the switch in pixels.
设置switch控件的最小宽度(以像素为单位)。

voidsetSwitchPadding(int pixels)

Set the amount of horizontal padding between the switch and the associated text.
设置开关和关联文字之间的水平内边距(padding)。

voidsetSwitchTextAppearance(Context context, int resid)

Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
设置指定TextAppearance资源中的switch控件的文本颜色,大小,样式,提示文本颜色和高亮颜色。

voidsetSwitchTypeface(Typeface tf)

Sets the typeface in which the text should be displayed on the switch.
设置文本应该显示在开关上的字体。

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.
设置在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.
设置按钮被选中状态下的显示文本。

voidsetThumbDrawable(Drawable thumb)

Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
设置switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

voidsetThumbResource(int resId)

Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
设置switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

voidsetThumbTextPadding(int pixels)

Set the horizontal padding around the text drawn on the switch itself.
设置switch文本的水平内边距(padding)。

voidsetThumbTintList(ColorStateList tint)

Applies a tint to the thumb drawable.
滑块drawable的tint。

voidsetThumbTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbTintList(ColorStateList)} to the thumb drawable.
设置混合模式,用于由setThumbTintList(ColorStateList)指定的滑块drawable的着色。

voidsetTrackDrawable(Drawable track)

Set the drawable used for the track that the switch slides within.
设置轨道的drawable,其用于switch内部。

voidsetTrackResource(int resId)

Set the drawable used for the track that the switch slides within.
设置轨道的drawable,其用于switch内部。

voidsetTrackTintList(ColorStateList tint)

Applies a tint to the track drawable.
用于滑轨drawable的tint。

voidsetTrackTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackTintList(ColorStateList)} to the track drawable.
设置混合模式,用于由setTrackTintList(ColorStateList)指定的轨道drawable的着色。

voidtoggle()

Change the checked state of the view to the inverse of its current state
将视图的选中状态更改为与当前状态相反的状态。

保护方法

voiddrawableStateChanged()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
每当视图状态发生变化时, 都会调用此方法, 从而影响所显示的drawable的状态。

int[]onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.
生成此视图的新Drawable状态。

voidonDraw(Canvas canvas)

Implement this to do your drawing.
实现此方法以完成绘制。

voidonLayout(boolean changed, int left, int top, int right, int bottom)

Called from layout when this view should assign a size and position to each of its children.
当此视图分配大小和位置给它的每个子项时,从布局中调用。

booleanverifyDrawable(Drawable who)

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.
如果你的视图子类显示在其自己的drawable对象上,则应该重载此方法,并返回true,以显示它所显示的任何drawable的对象。

继承方法

完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Switch.html

原创粉丝点击