It is like a switch control, which has only two States: on and off: in IOS, there is a UISwitch control, which is as follows:
In android4.0, a control similar to this one is added: Switch, which is very visual and switches. As follows:
The class relationship diagram is as follows:
Java. lang. Object
Using android. view. View
Using android. widget. TextView
Using android. widget. Button
Using android. widget. CompoundButton
Using android. widget. Switch
Parent class: compoundButton.
Class Overview:
A Switch is a Switch control that can be switched in two states. You can drag "thumb" back and forth to select a Switch. You can also click the Switch status as you select the check box.
Main Methods:
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 shoshould be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provide does not have all the bits in the style that you specified.
Void setSwitchTypeface (Typeface tf)
Sets the typeface in which the text shoshould 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 (): I don't know what it means.
Source code in TextView:
[Java] <SPAN style = "FONT-FAMILY: System"> public int getCompoundDrawablePadding (){
Final Drawables dr = mDrawables;
Return dr! = Null? Dr. mDrawablePadding: 0;
} </SPAN>
<SPAN style = "FONT-FAMILY: System"> public int getCompoundDrawablePadding (){
Final Drawables dr = mDrawables;
Return dr! = Null? Dr. mDrawablePadding: 0;
} </SPAN> jumpDrawableToCurrentState (): Call the Drawable. jumpToCurrentState () method in the Switch-related Drawable operation.
The getTextOff (), getTextOn (), setTextOff (), and setTextOn () methods are relatively simple. They are used to set and obtain text values in unselected and selected States.
OnMeasure (): measure the width and height of the control for plotting.
OnTouchEvent (MotionEvent ev) implements this method to transmit touch screen motion events.
SetChecked () sets the Switch status (selected, not selected)
SetSwitchTextAppearance () set the font size
SetSwitchTextTypefaces: Set the font format
Let's take a look at google's official definition in/frameworks/base/core/res/values/styles. xml:
[Java] 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 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> can be defined in main. xml as follows:
[Java] <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 = "open"
Android: textOff = "off"
/> </SPAN>
<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 = "open"
Android: textOff = "off"
/> </SPAN> when switching the Switch status:
[Java] <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 when selecting
StatusText. setText ("open ");
} Else {
// Do some thing when not selected
StatusText. setText ("off ");
}
}
}); </SPAN>