Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ToggleButton 開關按鈕控件的使用方法

編輯:關於 Android 程式設計

ToggleButton 是一個帶有開/關狀態切換的按鈕控件,本文整理它的基本用法。

一、簡介

從類結構來看,ToggleButton 的父類是 CompoundButton,引包時留意一下。

二、使用方法

1、新建 ToggleButton 控件及物件:

private ToggleButton toggleButton1;
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

2、設置 setOnCheckedChangeListener 監聽:

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})

3、根據是否 checked 實現相應操作:

if (isChecked) {// 開
    linearLayout1.setOrientation(LinearLayout.VERTICAL);
} else {// 關
    linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
}

三、程式碼實例

效果圖(開狀態):

關狀態:

package fry;

import com.example.ToggleButtonDemo1.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class Activity01 extends Activity {
    private LinearLayout linearLayout1;
    private ToggleButton toggleButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity01);
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

        toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {// 開
                    linearLayout1.setOrientation(LinearLayout.VERTICAL);
                } else {// 關
                    linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
                }
            }
        });
    }
}

activity01.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textOn="橫向排列"
        android:textOff="縱向排列" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" />
    </LinearLayout>
</LinearLayout>

四、重點整理

  • android:checked="true":設置 ToggleButton 的初始狀態。
  • android:textOn:設置開啟時顯示的文字。
  • android:textOff:設置關閉時顯示的文字。
  • setOnCheckedChangeListener:監聽狀態切換。
  • onCheckedChanged 中的 isChecked 參數用來判斷開關狀態。

ToggleButton 非常適合用來做「開/關」這類二態切換的場景,搭配監聽器即可驅動對應的業務邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved