android基本控件ToggleButton&Switch使用指南
ToggleButton(開關(guān)按鈕)和Switch(開關(guān))講解:
一、核心屬性講解:
(1)ToggleButton
textOn:按鈕被選中的時(shí)候文字顯示
textOff:按鈕沒(méi)有被選中的時(shí)候文字顯示
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的時(shí)候文字是否顯示
android:showText:設(shè)置on/off的時(shí)候是否顯示文字,boolean
android:splitTrack:是否設(shè)置一個(gè)間隙,讓滑塊與底部圖片分隔,boolean
android:switchMinWidth:設(shè)置開關(guān)的最小寬度
android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔
android:textOff:按鈕沒(méi)有被選中時(shí)顯示的文字
android:textOn:按鈕被選中時(shí)顯示的文字
android:textStyle:文字風(fēng)格,粗體,斜體寫劃線那些
android:track:底部的圖片
android:thumb:滑塊的圖片
可以自己動(dòng)手試一試每一個(gè)屬性
在做一個(gè)藍(lán)牙開關(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)牙開啟中" />
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)滑動(dòng)開關(guān)效果
- Android 仿蘋果IOS6開關(guān)按鈕
- Android開發(fā)進(jìn)階自定義控件之滑動(dòng)開關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
- Android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼
- Android開發(fā)之開關(guān)按鈕控件ToggleButton簡(jiǎn)單用法示例
相關(guān)文章
Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路
這篇文章主要介紹了Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路,需要的朋友可以參考下2018-04-04
Android解決viewpager嵌套滑動(dòng)沖突并保留側(cè)滑菜單功能
這篇文章主要介紹了 解決viewpager嵌套滑動(dòng)沖突,并保留側(cè)滑菜單功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android序列化之Parcelable和Serializable的使用詳解
本篇文章主要介紹了Android序列化之Parcelable和Serializable的使用詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android中多行文本末尾添加圖片排版問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Android中多行文本末尾添加圖片排版問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
基于Android SQLiteOpenHelper && CRUD 的使用
本篇文章小編為大家介紹,基于Android SQLiteOpenHelper && CRUD的使用。需要的朋友可以參考一下2013-04-04
Android?Flutter實(shí)現(xiàn)彈簧動(dòng)畫交互的示例詳解
物理模擬可以讓應(yīng)用程序的交互感覺(jué)逼真和互動(dòng),本文章實(shí)現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動(dòng)的點(diǎn)移回中心,感興趣的可以了解一下2023-04-04
Flutter利用ORM框架簡(jiǎn)化本地?cái)?shù)據(jù)庫(kù)管理詳解
使用?sqflite?相對(duì)來(lái)說(shuō)還是有點(diǎn)復(fù)雜,比如遇到數(shù)據(jù)不兼容的時(shí)候需要手動(dòng)轉(zhuǎn)換,增加了不少繁瑣的代碼。本篇我們就來(lái)介紹一個(gè)?ORM?框架,來(lái)簡(jiǎn)化數(shù)據(jù)庫(kù)的管理,感興趣的可以了解一下2023-04-04

