android-UI组件实例大全(五)------开关按钮ToggleButton和开关Switch

来源:互联网 发布:ios游戏辅助软件 编辑:程序博客网 时间:2024/05/18 02:50

这两个组件的话因为比较简单,某些书都会直接略过,不过笔者觉得还是有比较介绍一下这两个组件:\

相信有朋友都不知道这两个组件是怎么样的,so,普及下,上图:

ToggleButton:开关按钮:,就是点击以后那个蓝色的底部会变暗,就是一个开关,蓝色的时候表示on,灰色off

Switch:开关:点击以后会左右移动,在右边的时候表示on,左边表示off


!!!这里要注意一下,switch是4.0以后才出现的,所以要修改一下androidmanifest.xml中的最低版本的minsdk 改为 14或以上,不然会报错



因为这两个都是Button的子类,所以继承了Button的大部分属性,这里的话只将特殊的属性

话不多说


代码:

main.xml文件:

[html] view plaincopyprint?
  1. <!-- 定义一个ToggleButton按钮 -->  
  2.     <ToggleButton  
  3.         android:id="@+id/TogBtn"  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content"  
  6.         android:textOn="声音开"  
  7.         android:textOff="声音关闭"  
  8.         android:checked="true"       
  9.     />  
  10.     <!-- 定义一个switch开关 -->  
  11.     <Switch  
  12.         android:id="@+id/switcher"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:textOn="横向排列"  
  16.         android:textOff="纵向排列"  
  17.         android:thumb="@drawable/btnon"  
  18.         android:checked="true"   
  19.     />  
  20.       
  21.     <Button  
  22.         android:id="@+id/btntest1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="测试按钮1"   
  26.     />  
  27.       
  28.     <Button  
  29.         android:id="@+id/btntest2"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:text="测试按钮2"   
  33.     />  
  34.       
  35.     <Button  
  36.         android:id="@+id/btntest3"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:text="测试按钮3"   
  40.     />  


MainActivity.java部分:

[java] view plaincopyprint?
  1. private ToggleButton toggle;  
  2.     private Switch switcher;  
  3.     private LinearLayout linear;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.           
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         toggle = (ToggleButton) findViewById(R.id.TogBtn);  
  11.         switcher = (Switch) findViewById(R.id.switcher);  
  12.         linear = (LinearLayout) findViewById(R.id.LinearLayout1);  
  13.           
  14.         switcher.setOnCheckedChangeListener(listener);  
  15.           
  16.           
  17.     }  
  18.       
  19.     private OnCheckedChangeListener listener = new OnCheckedChangeListener() {  
  20.           
  21.         @Override  
  22.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  23.             // TODO Auto-generated method stub  
  24.             if(isChecked)linear.setOrientation(0);  
  25.             else linear.setOrientation(1);  
  26.         }  
  27.     };   



运行截图:


代码解释:

在xml文件中分别定义了一个ToggleButton和Switch开关,设置了textoff和textOn时显示的文本,设置选择状态,并设置了三个普通按钮

在java主文件中实例化两个开关对象,findViewByid后,再new一个onCheckedChangeListener对象,为switch添加触发事件;

setOrientation():指定linearlayout的方向,0和1,默认是horizontal横向排列,即0的时候!1的话就是竖直方向vertical

0 0
原创粉丝点击