您的位置:首页 > 产品设计 > UI/UE

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

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

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

ToggleButton:开关按钮:

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

Switch:开关:

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

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

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

话不多说

代码:

main.xml文件:

[html] view
plaincopyprint?

<!-- 定义一个ToggleButton按钮 -->  

    <ToggleButton  

        android:id="@+id/TogBtn"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:textOn="声音开"  

        android:textOff="声音关闭"  

        android:checked="true"       

    />  

    <!-- 定义一个switch开关 -->  

    <Switch  

        android:id="@+id/switcher"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:textOn="横向排列"  

        android:textOff="纵向排列"  

        android:thumb="@drawable/btnon"  

        android:checked="true"   

    />  

      

    <Button  

        android:id="@+id/btntest1"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="测试按钮1"   

    />  

      

    <Button  

        android:id="@+id/btntest2"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="测试按钮2"   

    />  

      

    <Button  

        android:id="@+id/btntest3"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="测试按钮3"   

    />  

MainActivity.java部分:

[java] view
plaincopyprint?

private ToggleButton toggle;  

    private Switch switcher;  

    private LinearLayout linear;  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

          

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

          

        toggle = (ToggleButton) findViewById(R.id.TogBtn);  

        switcher = (Switch) findViewById(R.id.switcher);  

        linear = (LinearLayout) findViewById(R.id.LinearLayout1);  

          

        switcher.setOnCheckedChangeListener(listener);  

          

          

    }  

      

    private OnCheckedChangeListener listener = new OnCheckedChangeListener() {  

          

        @Override  

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  

            // TODO Auto-generated method stub  

            if(isChecked)linear.setOrientation(0);  

            else linear.setOrientation(1);  

        }  

    };   

运行截图:



代码解释:

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

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

setOrientation():指定linearlayout的方向,0和1,默认是horizontal横向排列,即0的时候!1的话就是竖直方向vertical
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: