ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本。

    ToggleButton常用的XML属性

属性名称

描述

android:disabledAlpha

设置按钮在禁用时透明度。

  Android开发学习笔记:浅谈ToggleButton_第1张图片

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">测试ToggleButtonstring> 
  5. resources> 

效果图:

Android开发学习笔记:浅谈ToggleButton_第2张图片

第二个例子通过图片的变化显示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 p_w_picpathView;     
  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.      p_w_picpathView=(ImageView) findViewById(R.id.p_w_picpathView);          
  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.                 p_w_picpathView.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/p_w_picpathView"          
  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> 

效果图:

Android开发学习笔记:浅谈ToggleButton_第3张图片Android开发学习笔记:浅谈ToggleButton_第4张图片

更多相关文章

  1. Android控件之利用selector自定义的带文字的图片按钮
  2. Android 8.0 SystemUI下拉状态栏快捷开关
  3. Android的按钮监听事件&自定义回调函数
  4. Android Alert Dialog解决点击按钮对话框不关闭的问题
  5. Android使用selector自定义按钮
  6. Android Nine Patch图片及按钮背景
  7. Android 基本按钮
  8. Android 应用程序窗体显示状态操作(requestWindowFeature()的应用
  9. Android中控件的显示和隐藏以及EditText的可编辑和不可编辑状态

随机推荐

  1. css伪类选择器,字体图标,盒模型及自适应布
  2. Linux系统配置(系统优化)
  3. Nginx配置SSL和WSS步骤介绍
  4. 伪类选择器:元素匹配过程
  5. 字体图标的引入详细步骤(icon网站)
  6. 选择器的权重,为什么推荐class而不用id
  7. 配置云服务器应该怎么选择?有哪些注意事项
  8. 细说伪类选择器
  9. 人体该怎么画?漫画人物的人体画法
  10. css中的伪类选择器,字体图标与盒模以及常