1. Switch組件介紹
它是由API 14(Android 4.0, 4.0.1, 4.0.2)引入的新組建,是一種“組合按鈕”,即繼承了CompoundButton。就像CheckBox,RadioButton, 及ToggleButton一樣, 它擁有兩種狀態分別表示“開啟”和“關閉”。可以通過點擊和拖動來切換狀態,默認情況下,每個狀態上有一個用來顯示當前狀態的文本信息,比如,“ON”和“OFF”,不過也可以根據其控制的功能來自定義其顯示文本。
2. 使用Switch組件
使用該組件時,應該重點關注在其狀態發生變化時我們應該作何反應。即,我們需要監聽switch組件的狀態的變化。很幸運,合理的需求和想像大都可以得到滿足,switch的基類內部類CompoundButton.OnCheckedChangeListener幫了我們一個大忙。所以,我們的活動在使用switch時,可以實現CompoundButton.OnCheckedChangeListener接口,並實現其內部的onCheckedChanged方法。
除了關注Switch的狀態變化外,我們可以做的更多,比如可以改變組件的外觀。或許下面方法和屬性可以實現這一點:
- setSwitchTextAppearance(Context context, int resid) 使用指定的資源id設置狀態標簽上的文字大小,類型,顏色等;
- setSwitchTypeface(Typeface tf, int style) 使用指定的字體類型庫內的指定類型來設置狀態標簽上的文字;
- setSwitchTypeface(Typeface tf) 使用指定字體類型庫內的固有類型來設置狀態標簽上的文字;
- setTextOff(CharSequence textOff) 設置“關閉”狀態標簽文字;
- setTextOn(CharSequence textOn) 設置“開啟”狀體標簽文字;
- 父類內的setButtonDrawable(int resid) 用指定的資源id設置組件背景;
- 父類內的setButtonDrawable(Drawable d) 用可繪制對象設置組件背景;
android:textStyle的值必須是下面中的一個,或是它們的組合(|):
| 串 | 值 |
| normal | 0 |
| bold | 1 |
| italic | 2 |
android:typeface的值必須是下面中的一個:
| 串 | 值 |
| normal | 0 |
| sans | 1 |
| serif | 2 |
| monospace | 3 |
3. 實例代碼
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Switch android:text="Standard switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dip" /> <Switch android:text="Default is on" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dip" /> <Switch android:id="@+id/monitored_switch" android:text="Monitored switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dip" /> <Switch android:text="Customized text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="開啟" android:textOff="關閉" android:layout_marginBottom="32dip" /> <Switch android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be pinned at the top." android:singleLine="false" android:layout_width="300dip" android:layout_height="wrap_content" android:gravity="top|left" android:layout_marginBottom="32dip" /> <Switch android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be vertically centered." android:singleLine="false" android:layout_width="300dip" android:layout_height="wrap_content" android:gravity="center_vertical|left" android:layout_marginBottom="32dip" /> <Switch android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be pinned at the bottom." android:singleLine="false" android:layout_width="300dip" android:layout_height="wrap_content" android:gravity="bottom|left" android:layout_marginBottom="32dip" /> <Switch android:text="Switch with match_parent width" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="32dip" /> <TextView android:text="Standalone switch below:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
主程序:
package com.example.androidswitchtest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends Activity implements OnCheckedChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Switch s = (Switch) findViewById(R.id.monitored_switch); if (s != null) { s.setOnCheckedChangeListener(this); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(this, "Monitored switch is " + (arg1 ? "on" : "off"), Toast.LENGTH_SHORT).show(); } }
效果圖:

本文轉載自:http://www.cnblogs.com/MMLoveMeMM/articles/3350893.html
