歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 4.0新控件Switch方法解析

Android 4.0新控件Switch方法解析

日期:2017/3/1 10:19:16   编辑:Linux編程
就是很像開關的那種控件,它只有兩個狀態: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父類:compoundButton。

類的概述:

Switch是一個可以在兩種狀態切換的開關控件。用戶可以拖動"thumb"來回選擇,也可以像選擇復選框一樣點擊切換Switch的狀態。

主要方法:

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 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. void setSwitchTypeface(Typeface tf) Sets the typeface in which the text should 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():沒弄清楚什麼意思。

在TextView中的源碼:

  1. <span >public int getCompoundDrawablePadding() {
  2. final Drawables dr = mDrawables;
  3. return dr != null ? dr.mDrawablePadding : 0;
  4. }</span>
jumpDrawableToCurrentState():在與Switch相關的Drawable操作時調用 Drawable.jumpToCurrentState()這個方法。

getTextOff()、getTextOn()、 setTextOff()、setTextOn()這四個方法比較簡單,就是設定和獲取非選中和選中狀態下的文本值。

Copyright © Linux教程網 All Rights Reserved