android基本控件ToggleButton&Switch使用指南
ToggleButton(開關(guān)按鈕)和Switch(開關(guān))講解:
一、核心屬性講解:
(1)ToggleButton
textOn:按鈕被選中的時候文字顯示
textOff:按鈕沒有被選中的時候文字顯示
ToggleButton的狀態(tài)只能是選中和未選中,并且需要為不同的狀態(tài)設(shè)置不同的顯示文本。
以下案例為ToggleButton的用法
目錄結(jié)構(gòu)

main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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="關(guān)燈"
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);
}
});
}
}
運(yùn)行效果:

(2)switch:
showText:設(shè)置textOn/off的時候文字是否顯示
android:showText:設(shè)置on/off的時候是否顯示文字,boolean
android:splitTrack:是否設(shè)置一個間隙,讓滑塊與底部圖片分隔,boolean
android:switchMinWidth:設(shè)置開關(guān)的最小寬度
android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔
android:textOff:按鈕沒有被選中時顯示的文字
android:textOn:按鈕被選中時顯示的文字
android:textStyle:文字風(fēng)格,粗體,斜體寫劃線那些
android:track:底部的圖片
android:thumb:滑塊的圖片
可以自己動手試一試每一個屬性
在做一個藍(lán)牙開關(guān)時候,用到了switch,記一下用法,其實(shí)跟Button是幾乎一樣的.
布局中:
<Switch
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="藍(lán)牙關(guān)閉中"
android:textOn="藍(lán)牙開啟中" />
java代碼中
open.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
mBluetoothAdapter.enable();//打開藍(lán)牙
} else {
mBluetoothAdapter.disable();// 關(guān)閉藍(lán)牙
}
}
});
就是這樣了,一看就明白了.

- Android控件之ToggleButton的使用方法
- Android控件ToggleButton多狀態(tài)按鈕使用詳解
- Android ToggleButton 詳解及實(shí)例代碼
- Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
- Android開發(fā)之ToggleButton實(shí)現(xiàn)開關(guān)效果示例
- Android自定義實(shí)現(xiàn)開關(guān)按鈕代碼
- Android自定義控件實(shí)現(xiàn)滑動開關(guān)效果
- Android 仿蘋果IOS6開關(guān)按鈕
- Android開發(fā)進(jìn)階自定義控件之滑動開關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
- Android開發(fā)仿IOS滑動開關(guān)實(shí)現(xiàn)代碼
- Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例
相關(guān)文章
Android實(shí)現(xiàn)個人資料頁面頭像背景模糊顯示包(狀態(tài)欄)
這篇文章主要介紹了Android實(shí)現(xiàn)個人資料頁面頭像背景模糊顯示包括狀態(tài)欄,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
AndroidManifest.xml uses-feature功能詳解
這篇文章主要介紹了AndroidManifest.xml uses-feature功能,較為詳細(xì)的分析了Android屬性過濾操作的功能與相關(guān)技巧,需要的朋友可以參考下2016-10-10
淺析Android 快速實(shí)現(xiàn)圖片壓縮與上傳功能
在Android對手機(jī)相冊中的圖片的壓縮和上傳到服務(wù)器上,這樣的功能在每個app開發(fā)中都會有這樣的需求.所以今天就對android端怎么快速實(shí)現(xiàn)圖片壓縮和上傳進(jìn)行簡單的分析2017-08-08
Android數(shù)據(jù)轉(zhuǎn)移之Launcher導(dǎo)出數(shù)據(jù)庫給另一臺機(jī)器加載
這篇文章主要介紹了Android系統(tǒng)中Launcher導(dǎo)出數(shù)據(jù)庫給另一臺機(jī)器加載的詳細(xì)流程,數(shù)據(jù)轉(zhuǎn)移是少見但早晚要用到的功能,感興趣的朋友快來提前掌握吧2021-11-11
Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)
這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲不同的數(shù)據(jù)類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細(xì)介紹該內(nèi)容,需要的朋友可以參考一下2021-12-12
Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
這篇文章主要介紹了Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程,結(jié)合實(shí)例形式詳細(xì)分析了Android6.0屏幕旋轉(zhuǎn)的原理與相關(guān)實(shí)現(xiàn)流程,并附帶了Android動態(tài)開啟與禁用屏幕旋轉(zhuǎn)的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-11-11

