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


      狀態開關按鈕(ToggleButton)與開關(Switch)也是由Button派生出來的,因此它們的本質也是按鈕,Button支持的各種屬性、方法也適用於ToggleButton和Switch。從功能上來看,ToggleButton、Switch與CheckBox復選框非常相似,它們都可以提供兩個狀態。不過ToggleButton、Switch與CheckBox的區別主要體現在功能上,ToggleButton、Switch通常用於切換程序中的某種狀態。

      表2.14顯示了ToggleButton所支持的XML屬性及相關方法的說明。

    表2.14 ToggleButton支持的XML屬性及相關方法說明

XML屬性 相關方法 說明
android:checked setChecked(boolean) 設置該按鈕是否被選中
android:textOff   設置當該按鈕的狀態關閉時顯示的文本
android:textOn   設置當該按鈕的狀態打開時顯示的文本

   表2.15顯示了Switch所支持的XML屬性及相關方法的說明。

    表2.15 Switch支持的XML屬性及相關方法說明

XML屬性 相關方法 說明
android:checked setChecked(boolean) 設置該開關是否被選中
android:switchMinWidth setSwitchMinWidth(int) 設置該開關的最小寬度
android:switchPadding setSwitchPadding(int) 設置開關與標題文本之間的空白
android:switchTextAppearance setSwitchTextAppearance(Context,int) 設置該開關圖標上的文本樣式
android:textOff setTextOff(CharSequence) 設置該開關的狀態關閉時顯示的文本
android:textOn setTextOn(CharSequence) 設置該開關的文本的風格
android:textStyle setSwitchTypeface(Typeface) 設置該開關的文本的風格
android:thumb setThumbResource(int) 指定使用自定義Drawable繪制該開關的開關按鈕
android:track setTrackResource(int) 指定使用自定義Drawable繪制該開關的開關軌道
android:typeface setSwitchTypeface(Typeface) 設置該開關的文本的字體風格

 實例:動態控制布局

    該實例的思路是在頁面中增加一個ToggleButton,隨着該按鈕狀態改變,界面布局中的LinearLayout布局的方向在水平布局、垂直布局之間切換。下面是該程序所使用的界面布局。

  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
<!-- 定義一個ToggleButton按鈕 -->
<ToggleButton android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="橫向排列"
    android:textOn="縱向排列"
    android:checked="true" />
<Switch android:id="@+id/switcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="橫向排列"
    android:textOn="縱向排列"
    android:thumb="@drawable/check"
    android:checked="true" />
<!-- 下面省略了三個按鈕的定義 -->
<LinearLayout android:orientation="vertical"
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <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>

 上面LinearLayout中定義了三個按鈕,該LinearLayout默認采用垂直方向的線性布局。接下來我們為ToggleButton按鈕、Switch按鈕綁定監聽器,當它的選中狀態發生改變時,程序通過代碼來改變LinearLayout的布局方向。

  

package org.crazyit.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class ToggleButtonTest extends Activity {
    ToggleButton toggle;
    Switch switcher;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toggle_button_test);
        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 arg0, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    //設置LinearLayout垂直布局
                    test.setOrientation(1);
                }
                else
                {
                    test.setOrientation(0);    
                }
            }

        };
        toggle.setOnCheckedChangeListener(listener);
        switcher.setOnCheckedChangeListener(listener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toggle_button_test, menu);
        return true;
    }

}

運行該Activity,隨着用戶改變ToggleButton按鈕的狀態,下面界面布局的方向也在不斷發生變化。圖2.26顯示了ToggleButton的界面。

圖2.26 ToggleButton與Switch的功能


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM