編輯:關於 Android 程式設計
ToggleButton(開關按鈕)與 Switch(開關)講解:
一、核心屬性講解:
(1)ToggleButton
textOn:按鈕被選中時顯示的文字
textOff:按鈕未被選中時顯示的文字
ToggleButton 的狀態只有選中與未選中兩種,並且需要為不同狀態設定不同的顯示文字。
以下案例示範 ToggleButton 的用法。
目錄結構

main.xml 佈局檔
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bulb_off" android:layout_gravity="center_horizontal" /> <ToggleButton android:id="@+id/toggleButton" android:layout_width="140dip" android:layout_height="wrap_content" android:textOn="開燈" android:textOff="關燈" android:layout_gravity="center_horizontal" /> </LinearLayout>
ToggleButtonActivity 類
package com.ljq.tb; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ToggleButton; import android.widget.CompoundButton.OnCheckedChangeListener; public class ToggleButtonActivity extends Activity { private ImageView imageView=null; private ToggleButton toggleButton=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView=(ImageView) findViewById(R.id.imageView); toggleButton=(ToggleButton)findViewById(R.id.toggleButton); toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { toggleButton.setChecked(isChecked); imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off); } }); } } 運行效果:

(2)Switch:
android:showText:設定 on/off 時是否顯示文字,型別為 boolean
android:splitTrack:是否設定一個間隙,讓滑塊與底部圖片分隔,boolean
android:switchMinWidth:設定開關的最小寬度
android:switchPadding:設定滑塊內文字的間隔
android:textOff:按鈕未被選中時顯示的文字
android:textOn:按鈕被選中時顯示的文字
android:textStyle:文字風格,粗體、斜體、劃線等
android:track:底部的圖片
android:thumb:滑塊的圖片
這些屬性都可以自己動手逐一試試。
之前做一個藍牙開關時用到了 Switch,順手記一下用法,其實它和 Button 幾乎一樣。
佈局中:
<Switch android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="藍牙關閉中" android:textOn="藍牙開啟中" />
Java 程式碼中
open.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { mBluetoothAdapter.enable();//打開藍牙 } else { mBluetoothAdapter.disable();// 關閉藍牙 } } }); 就是這樣,一看就明白了。

用開源框架 SwipeListView 快速做出列表滑動刪除
近幾年微信、QQ 相繼帶起「滑動刪除」的風潮,這個互動幾乎成了清單類頁面的標準配備。網路上做過一次橫向比較:微信的實作看起來更像一個超出螢幕的單層布局,滑動時右側那顆原本
Android 4.4 的外部儲存新模型:二級存儲與新增的存取 API
Android 4.4 對外部儲存(尤其是外置 SD 卡)的權限模型做了重大調整,很多原本能正常寫檔的 App 升級後突然寫不進去了。本文說明規則變化的內容與對應的新 A
Android 仿微信短視頻拍攝:自訂錄製控制項完整實作
近期專案需要加入短視頻上傳功能,交互形式設定為類似微信:按住開始拍攝,設有最長時長限制,到達上限自動停止。研究一段時間後把功能跑通了,這裡把過程整理出來分享。 1. 視頻
Android 裝置插入印表機無法掛載:核心驅動缺少 DEVTYPE 的追蹤過程
原本我以為自己在解一個「印表機熱插拔」的問題,一路調下來才發現真正的癥結在完全不同的地方。最後決定還是回到最初的那個題目,因為那才是問題的起點。 現象 插入印表機後,l