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)擊的時候,執(zhí)行當(dāng)前方法
* buttonView 代表被點(diǎn)擊的控件本身
* isChecked 代表被點(diǎn)擊的控件的狀態(tài)
*
* 當(dāng)點(diǎn)擊tb的時候,更換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é)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- 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)滑動開關(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樣式的開發(fā):layer-list實(shí)例詳解
本文主要介紹Android樣式開發(fā)layer-list,這里整理了詳細(xì)的資料,及簡單示例代碼有興趣的小伙伴可以參考下2016-09-09
詳解Android Activity之間切換傳遞數(shù)據(jù)的方法
這篇文章主要介紹了詳解Android Activity之間切換傳遞數(shù)據(jù)的方法 的相關(guān)資料,需要的朋友可以參考下2016-04-04
android中Bitmap用法(顯示,保存,縮放,旋轉(zhuǎn))實(shí)例分析
這篇文章主要介紹了android中Bitmap用法,以實(shí)例形式較為詳細(xì)的分析了android中Bitmap操作圖片的顯示、保存、縮放、旋轉(zhuǎn)等相關(guān)技巧,需要的朋友可以參考下2015-09-09
Android軟鍵盤的顯示隱藏功能實(shí)現(xiàn)過程
這篇文章主要介紹了Android軟鍵盤的顯示隱藏功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
漂亮的Android音樂歌詞控件 仿網(wǎng)易云音樂滑動效果
這篇文章主要為大家詳細(xì)介紹了漂亮的Android音樂歌詞控件,仿網(wǎng)易云音樂滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
事件是一種有用來收集用戶與應(yīng)用程序互動數(shù)據(jù)的互動組件,如按鍵或觸摸屏等放置事件,因?yàn)槊總€事件從Android框架維護(hù)事件隊(duì)列先入先出(FIFO)基礎(chǔ)上的隊(duì)列??梢栽诔绦蛑胁东@這些事件,按要求并采取適當(dāng)?shù)膭幼?/div> 2023-02-02
Android 網(wǎng)絡(luò)請求框架Volley實(shí)例詳解
這篇文章主要介紹了Android 網(wǎng)絡(luò)請求框架Volley實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06最新評論

