Android控制項ToggleButton的用法,androidbutton用法

來源:互聯網
上載者:User

Android控制項ToggleButton的用法,androidbutton用法
ToggleButton(開關按鈕)是Android系統中比較簡單的一個組件,是一個具有選中和未選擇狀態雙狀態的按鈕,並且需要為不同的狀態設定不同的顯示文本。

 ToggleButton常用的XML屬性

屬性名稱

描述

android:disabledAlpha

設定按鈕在禁用時透明度。

 

android:textOff

未選中時按鈕的文本

android:textOn

選中時按鈕的文本



main.xml:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:orientation="vertical" android:layout_width="fill_parent" android:background="#FFF5F5F5"          android:layout_height="fill_parent">          <LinearLayout android:layout_width="fill_parent"                  android:layout_height="wrap_content" android:orientation="horizontal">                  <TextView android:textSize="14.0sp" android:id="@+id/tvSound" android:textColor="@android:color/black"                          android:layout_width="wrap_content" android:layout_height="wrap_content"                          android:text="已開啟" />                  <ToggleButton                           android:id="@+id/tglSound" android:background="@drawable/selector_butn_toggle"                          android:layout_width="wrap_content" android:layout_height="wrap_content"                          android:checked="true" android:textOn="" android:textOff=""                           android:text="" />          </LinearLayout>  </LinearLayout>

  

MainActivity.java:

package com.apkbus.toggle;    import android.app.Activity;  import android.os.Bundle;  import android.view.Window;  import android.widget.CompoundButton;  import android.widget.CompoundButton.OnCheckedChangeListener;  import android.widget.TextView;  import android.widget.ToggleButton;    public class MainActivity extends Activity implements OnCheckedChangeListener{          private ToggleButton mToggleButton;          private TextView tvSound;          @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題列          setContentView(R.layout.main);          initView();//初始化控制項方法      }            private void initView() {                  mToggleButton = (ToggleButton) findViewById(R.id.tglSound); //擷取到控制項                  mToggleButton.setOnCheckedChangeListener(this);//添加監聽事件                  tvSound = (TextView) findViewById(R.id.tvSound);          }            @Override          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                  if(isChecked){                          tvSound.setText("已開啟");                  }else{                          tvSound.setText("已關閉");                  }          }  }  


 

Demo:http://download.csdn.net/detail/u010963246/8941969

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.