Android開發(fā)之ToggleButton實(shí)現(xiàn)開關(guān)效果示例
本文實(shí)例講述了Android使用ToggleButton實(shí)現(xiàn)開關(guān)效果的方法。分享給大家供大家參考,具體如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ToggleButton android:checked="false" android:textOn="開" android:textOff="關(guān)" android:id="@+id/toggleButton1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/off" /> </LinearLayout>
MainActivity.java
package com.example.hello; import android.support.v7.app.ActionBarActivity; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.ToggleButton; import android.os.Bundle; public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener { private ToggleButton tb; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 tb = (ToggleButton) findViewById(R.id.toggleButton1); img = (ImageView) findViewById(R.id.imageView1); //給當(dāng)前的tb設(shè)置監(jiān)聽器 tb.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { /* * 當(dāng)tb被點(diǎn)擊的時(shí)候,執(zhí)行當(dāng)前方法 * buttonView 代表被點(diǎn)擊的控件本身 * isChecked 代表被點(diǎn)擊的控件的狀態(tài) * * 當(dāng)點(diǎn)擊tb的時(shí)候,更換img的背景 */ img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android控件之ToggleButton的使用方法
- android基本控件ToggleButton&Switch使用指南
- Android控件ToggleButton多狀態(tài)按鈕使用詳解
- Android ToggleButton 詳解及實(shí)例代碼
- Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
- 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)文章
一個(gè)Activity中多個(gè)Fragment的切換
經(jīng)常會(huì)遇到在一個(gè)activity界面上布局多個(gè)fragment,但是如何從一個(gè)fragment跳轉(zhuǎn)到另一個(gè)fragment呢?本文主要對(duì)一個(gè)Activity中多個(gè)Fragment的切換進(jìn)行介紹,下面跟著小編一起來看下吧2017-01-01Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法,基于繼承BaseAdapter自定義Gallery和ImageAdapter實(shí)現(xiàn)倒影的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
這篇文章主要介紹了Android自定義可拖拽的懸浮按鈕DragFloatingActionButton,需要的朋友可以參考下2017-06-06Android Activity啟動(dòng)模式之standard實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之standard,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中活動(dòng)(Activity)四種啟動(dòng)模式中的standard相關(guān)注意事項(xiàng)與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)帶磁性的懸浮窗體效果
這篇文章主要介紹了Android實(shí)現(xiàn)帶磁性的懸浮窗體效果,涉及Android針對(duì)窗體的動(dòng)態(tài)操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android 自定義輸入支付密碼的軟鍵盤實(shí)例代碼
這篇文章主要介紹了Android 自定義輸入支付密碼的軟鍵盤實(shí)例代碼的相關(guān)資料,并附簡(jiǎn)單實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11使用Messenger實(shí)現(xiàn)Service的雙向通信
這篇文章主要為大家詳細(xì)介紹了使用Messenger實(shí)現(xiàn)Service的雙向通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android SdkVersion的區(qū)別及獲取版本信息方法
下面小編就為大家?guī)硪黄狝ndroid SdkVersion的區(qū)別及獲取版本信息方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03