1. 程式人生 > >狀態開關按鈕(ToggleButton)和開關(Switch)的功能與用法

狀態開關按鈕(ToggleButton)和開關(Switch)的功能與用法

狀態開關按鈕(ToggleButton)和開關(Switch)也是由Button派生出來的,因此他們的本質也是那妞,Button支援的各種屬性、方法也適用於ToggleButton和Switch。

從功能上看,ToggleButton、Switch和CheckBox複選框非常相似,他們都可以提供兩種狀態。不過ToggleButton、Switch與CheckBox的區別主要體現在功能上,ToggleButton、Switch通常用於切換程式中的某種狀態。

例項:動態控制佈局。

介面佈局檔案如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.l2112.togglebuttontest.MainActivity" tools:showIn="@layout/activity_main" android:orientation="vertical"
> <!--定義一個ToggleButton按鈕--> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/toggle" android:textOff="橫向排列" android:textOn="縱向排列" android:checked="true"/> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/switcher" android:textOff="橫向排列" android:textOn="縱向排列" android:thumb="@drawable/check" android:checked="true"/> <!--定義一個可以動態改變方向的線性佈局--> <LinearLayout android:id="@+id/test" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕一"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕二"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕三"/> </LinearLayout> </LinearLayout>
MainActivity.java程式碼如下。
package com.example.l2112.togglebuttontest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;
import com.example.l2112.togglebuttontest.R;
public class MainActivity extends Activity
{
    ToggleButton toggle;
Switch switcher;
@Override
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
toggle = (ToggleButton)findViewById(R.id.toggle);
switcher = (Switch)findViewById(R.id.switcher);
        final LinearLayout test = (LinearLayout)findViewById(R.id.test);
OnCheckedChangeListener listener = new OnCheckedChangeListener()
        {
            @Override
public void onCheckedChanged(CompoundButton button
                    , boolean isChecked)
            {
                if(isChecked)
                {
                    // 設定LinearLayout垂直佈局
test.setOrientation(LinearLayout.VERTICAL);
toggle.setChecked(true);
switcher.setChecked(true);
}
                else
{
                    // 設定LinearLayout水平佈局
test.setOrientation(LinearLayout.HORIZONTAL);
toggle.setChecked(false);
switcher.setChecked(false);
}
            }
        };
toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
}
執行結果如下。