android基本控件ToggleButton&Switch使用指南
ToggleButton(開(kāi)關(guān)按鈕)和Switch(開(kāi)關(guān))講解:
一、核心屬性講解:
(1)ToggleButton
textOn:按鈕被選中的時(shí)候文字顯示
textOff:按鈕沒(méi)有被選中的時(shí)候文字顯示
ToggleButton的狀態(tài)只能是選中和未選中,并且需要為不同的狀態(tài)設(shè)置不同的顯示文本。
以下案例為T(mén)oggleButton的用法
目錄結(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="開(kāi)燈"
android:textOff="關(guān)燈"
android:layout_gravity="center_horizontal" />
</LinearLayout>
ToggleButtonActivity類(lèi)
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的時(shí)候文字是否顯示
android:showText:設(shè)置on/off的時(shí)候是否顯示文字,boolean
android:splitTrack:是否設(shè)置一個(gè)間隙,讓滑塊與底部圖片分隔,boolean
android:switchMinWidth:設(shè)置開(kāi)關(guān)的最小寬度
android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔
android:textOff:按鈕沒(méi)有被選中時(shí)顯示的文字
android:textOn:按鈕被選中時(shí)顯示的文字
android:textStyle:文字風(fēng)格,粗體,斜體寫(xiě)劃線那些
android:track:底部的圖片
android:thumb:滑塊的圖片
可以自己動(dòng)手試一試每一個(gè)屬性
在做一個(gè)藍(lán)牙開(kāi)關(guān)時(shí)候,用到了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)牙開(kāi)啟中" />
java代碼中
open.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
mBluetoothAdapter.enable();//打開(kāi)藍(lán)牙
} else {
mBluetoothAdapter.disable();// 關(guān)閉藍(lán)牙
}
}
});
就是這樣了,一看就明白了.

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

