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);
}
});
}
}
運行效果:

(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:滑塊的圖片
可以自己動手試一試每一個屬性
在做一個藍牙開關(guān)時候,用到了switch,記一下用法,其實跟Button是幾乎一樣的.
布局中:
<Switch
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="藍牙關(guān)閉中"
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();// 關(guān)閉藍牙
}
}
});
就是這樣了,一看就明白了.

- Android控件之ToggleButton的使用方法
- Android控件ToggleButton多狀態(tài)按鈕使用詳解
- Android ToggleButton 詳解及實例代碼
- Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
- Android開發(fā)之ToggleButton實現(xiàn)開關(guān)效果示例
- Android自定義實現(xiàn)開關(guān)按鈕代碼
- Android自定義控件實現(xiàn)滑動開關(guān)效果
- Android 仿蘋果IOS6開關(guān)按鈕
- Android開發(fā)進階自定義控件之滑動開關(guān)實現(xiàn)方法【附demo源碼下載】
- Android開發(fā)仿IOS滑動開關(guān)實現(xiàn)代碼
- Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例
相關(guān)文章
Android實現(xiàn)鍵盤彈出界面上移的實現(xiàn)思路
這篇文章主要介紹了Android實現(xiàn)鍵盤彈出界面上移的實現(xiàn)思路,需要的朋友可以參考下2018-04-04
Android解決viewpager嵌套滑動沖突并保留側(cè)滑菜單功能
這篇文章主要介紹了 解決viewpager嵌套滑動沖突,并保留側(cè)滑菜單功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Android序列化之Parcelable和Serializable的使用詳解
本篇文章主要介紹了Android序列化之Parcelable和Serializable的使用詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
基于Android SQLiteOpenHelper && CRUD 的使用
本篇文章小編為大家介紹,基于Android SQLiteOpenHelper && CRUD的使用。需要的朋友可以參考一下2013-04-04
Android?Flutter實現(xiàn)彈簧動畫交互的示例詳解
物理模擬可以讓應(yīng)用程序的交互感覺逼真和互動,本文章實現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動的點移回中心,感興趣的可以了解一下2023-04-04
Flutter利用ORM框架簡化本地數(shù)據(jù)庫管理詳解
使用?sqflite?相對來說還是有點復(fù)雜,比如遇到數(shù)據(jù)不兼容的時候需要手動轉(zhuǎn)換,增加了不少繁瑣的代碼。本篇我們就來介紹一個?ORM?框架,來簡化數(shù)據(jù)庫的管理,感興趣的可以了解一下2023-04-04

