Android开发学习笔记:浅谈ToggleButton

来源:互联网 发布:婚礼策划 知乎 编辑:程序博客网 时间:2024/05/16 06:35

    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


原创粉丝点击