android 4.0新控制項 Switch 方法解析

來源:互聯網
上載者:User

就是很像開關的那種控制項,它只有兩個狀態: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中的源碼:

[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():在與Switch相關的Drawable操作時調用 Drawable.jumpToCurrentState()這個方法。


 

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

     onMeasure():測量控制項寬高,供繪圖時使用。

     onTouchEvent(MotionEvent ev)實現這一方法傳遞觸控螢幕運動事件。

setChecked()設定Switch的狀態(選中,非選中)

setSwitchTextAppearance()設定字型大小

setSwitchTextTypefaces設定字型格式


看看google官方在/frameworks/base/core/res/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> 

<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中這樣定義:

[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="開" 
        android:textOff="關" 
        /></SPAN></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="開"
        android:textOff="關"
        /></SPAN></SPAN>當Switch狀態切換時:

[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   
                    statusText.setText("開"); 
                } else { 
                    //非選中時 do some thing   
                    statusText.setText("關"); 
                } 
                 
            } 
        });</SPAN></SPAN> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.