原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://liangruijun.blog.51cto.com/3061169/655014

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

    ToggleButton常用的XML屬性

 

屬性名稱

描述

android:disabledAlpha

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

 

android:textOff

未選中時按鈕的文本

android:textOn

選中時按鈕的文本

 

下面是具體的例子:

第一個例子是通過Toast顯示ToggleButton不同的狀態時的信息

MainActivity.java

  1. package com.android.togglebutton;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Toast;  
  8. import android.widget.ToggleButton;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     //聲明ToggleButton  
  12.     private ToggleButton togglebutton;  
  13.     @Override 
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         togglebutton = (ToggleButton) findViewById(R.id.togglebutton);  
  19.         togglebutton.setOnClickListener(new OnClickListener() {      
  20.             public void onClick(View v) {          
  21.                 // 當按鈕第一次被點擊時候響應的事件        
  22.                 if (togglebutton.isChecked()) {              
  23.                     Toast.makeText(MainActivity.this"你喜歡球類運動", Toast.LENGTH_SHORT).show();         
  24.                 }   
  25.                 // 當按鈕再次被點擊時候響應的事件  
  26.                 else {              
  27.                     Toast.makeText(MainActivity.this"你不喜歡球類運動", Toast.LENGTH_SHORT).show();          
  28.                 }      
  29.             }  
  30.           });  
  31.     }  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello" 
  11.         /> 
  12.     <ToggleButton   
  13.         android:id="@+id/togglebutton"          
  14.         android:layout_width="wrap_content"          
  15.         android:layout_height="wrap_content"          
  16.         android:textOn="喜歡"          
  17.         android:textOff="不喜歡" 
  18.         /> 
  19. </LinearLayout> 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">你喜不喜歡球類運動?</string> 
  4.     <string name="app_name">測試ToggleButton</string> 
  5. </resources> 

效果圖:

 

第二個例子通過圖片的變化顯示ToggleButton不同的狀態時的圖片

MainActivity.java

  1. package com.android.togglebutton;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.CompoundButton.OnCheckedChangeListener;  
  7. import android.widget.ImageView;  
  8. import android.widget.ToggleButton;  
  9.  
  10. public class MainActivity extends Activity {  
  11.      //聲明ImageView,ToggleButton  
  12.     private ImageView imageView;     
  13.     private ToggleButton toggleButton;   
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) {          
  16.      super.onCreate(savedInstanceState);          
  17.      setContentView(R.layout.main);   
  18.      //通過findViewById獲得ImageView,ToggleButton  
  19.      imageView=(ImageView) findViewById(R.id.imageView);          
  20.      toggleButton=(ToggleButton)findViewById(R.id.toggleButton);   
  21.        
  22.      toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){              
  23.       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {   
  24.        toggleButton.setChecked(isChecked);  
  25.                 //使用三目運算符來響應按鈕變換的事件  
  26.                 imageView.setImageResource(isChecked?R.drawable.pic_on:R.drawable.pic_off);  
  27.          }                      
  28.       });      
  29.     }  
  30. }  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      
  3.       android:orientation="vertical"      
  4.       android:layout_width="fill_parent"      
  5.       android:layout_height="fill_parent">      
  6.   <ImageView   
  7.       android:id="@+id/imageView"          
  8.       android:layout_width="wrap_content"          
  9.       android:layout_height="wrap_content"          
  10.       android:src="@drawable/pic_off"           
  11.       android:layout_gravity="center_horizontal"   
  12.       />      
  13.    <ToggleButton   
  14.       android:id="@+id/toggleButton"          
  15.       android:layout_width="130dip"          
  16.       android:layout_height="wrap_content"          
  17.       android:textOn="開燈"          
  18.       android:textOff="關燈"          
  19.       android:layout_gravity="center_horizontal"   
  20.       /> 
  21. </LinearLayout> 

效果圖:

 

本文出自 「IT的點點滴滴」 博客,請務必保留此出處http://liangruijun.blog.51cto.com/3061169/655014

arrow
arrow
    全站熱搜

    Felix 發表在 痞客邦 留言(0) 人氣()