- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境設定
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動(Activity)
- Android - 服務(Service)
- Android - 廣播接收器(Broadcast Receiver)
- Android - 內容提供器(Content Provider)
- Android - 碎片(Fragment)
- Android - 意圖/過濾器(Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - AudioManager
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入旋轉器
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - Shared Preferences
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - ToggleButton 控制元件
ToggleButton 以按鈕的形式顯示選中/未選中狀態。它基本上是一個帶指示燈的開/關按鈕。
切換按鈕
ToggleButton 屬性
以下是與 ToggleButton 控制元件相關的重要的屬性。您可以檢視 Android 官方文件以獲取屬性的完整列表以及相關方法,您可以使用這些方法在執行時更改這些屬性。
| 序號 | 屬性 & 描述 |
|---|---|
| 1 |
android:disabledAlpha 這是停用時應用於指示器的 Alpha 值。 |
| 2 |
android:textOff 這是按鈕未選中時的文字。 |
| 3 |
android:textOn 這是按鈕選中時的文字。 |
繼承自 android.widget.TextView 類 -
| 序號 | 屬性 & 描述 |
|---|---|
| 1 |
android:autoText 如果設定,則指定此 TextView 具有文字輸入方法並自動更正一些常見的拼寫錯誤。 |
| 2 |
android:drawableBottom 這是要在文字下方繪製的可繪製物件。 |
| 3 |
android:drawableRight 這是要在文字右側繪製的可繪製物件。 |
| 4 |
android:editable 如果設定,則指定此 TextView 具有輸入方法。 |
| 5 |
android:text 這是要顯示的文字。 |
繼承自 android.view.View 類 -
| 序號 | 屬性 & 描述 |
|---|---|
| 1 |
android:background 這是用作背景的可繪製物件。 |
| 2 |
android:contentDescription 這定義了簡要描述檢視內容的文字。 |
| 3 |
android:id 這為該檢視提供一個識別符號名稱, |
| 4 |
android:onClick 這是此檢視上下文中的方法名稱,當單擊檢視時呼叫該方法。 |
| 5 |
android:visibility 這控制檢視的初始可見性。 |
示例
此示例將引導您完成簡單的步驟,以演示如何使用線性佈局和 ToggleButton 建立自己的 Android 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio IDE 建立一個 Android 應用程式,並將其命名為 My Application,放在包 com.example.saira_000.myapplication 下,如Hello World 示例章節中所述。 |
| 2 | 修改 src/MainActivity.java 檔案以新增單擊事件。 |
| 2 | 修改 res/layout/activity_main.xml 檔案的預設內容以包含 Android UI 控制元件。 |
| 3 | 無需宣告預設常量。Android Studio 會在 string.xml 中處理預設常量。 |
| 4 | 執行應用程式以啟動 Android 模擬器並驗證對應用程式所做的更改的結果。 |
以下是修改後的主活動檔案 src/MainActivity.java 的內容。此檔案可以包含每個基本生命週期方法。
在下面的示例中,abc 指示 tutorialspoint 的影像
package com.example.saira_000.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends ActionBarActivity {
ToggleButton tg1,tg2;
Button b1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tg1=(ToggleButton)findViewById(R.id.toggleButton);
tg2=(ToggleButton)findViewById(R.id.toggleButton2);
b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("You have clicked first ON Button-:) ").append(tg1.getText());
result.append("You have clicked Second ON Button -:) ").append(tg2.getText());
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
以下是 res/layout/activity_main.xml 檔案的內容 -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="@+id/toggleButton"
android:checked="true"
android:layout_below="@+id/imageButton"
android:layout_toEndOf="@+id/button2/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="@+id/toggleButton2"
android:checked="true"
android:layout_alignTop="@+id/toggleButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="ClickMe"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是 res/values/strings.xml 檔案的內容,用於定義這些新的常量 -
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 的預設內容 -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saira_000.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
讓我們嘗試執行您的 My Application 應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟專案中的一個活動檔案,然後單擊工具欄中的執行
圖示。Android Studio 將應用程式安裝到您的 AVD 上並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗 -
將顯示以下螢幕 -
如果您先單擊了按鈕,您將在 Toast 上收到一條訊息 您單擊了第一個 ON 按鈕 -:),否則,如果您單擊了第二個按鈕,您將在 Toast 上收到一條訊息 您單擊了第二個 ON 按鈕 -:)
練習
我建議您嘗試使用佈局 XML 檔案以及程式設計時 ToggleButton 的不同屬性來嘗試上述示例,以獲得 ToggleButton 不同的外觀和感覺。嘗試使其可編輯,更改字型顏色、字體系列、寬度、textSize 等,並檢視結果。您還可以嘗試在單個活動中使用多個 ToggleButton 控制元件的上述示例。
