zoukankan      html  css  js  c++  java
  • Android基础控件ToggleButton和Switch开关按钮

    1、简介

      ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用!

    ToggleButton
            <!--checked  是否选择-->
            <!--disabledAlpha  禁用时透明度-->
            <!--textOn,textOff  打开/关闭时文字-->
    
    Switch
            <!--checked  是否选择-->
            <!--textOn,textOff  打开/关闭时文字-->
            <!--showText 是否显示文字-->
            <!--splitTrack 滑块是否和底部图片分隔-->
            <!--switchMinWidth 最小宽度-->
            <!--track 底部图片-->
            <!--thumb 滑块图片-->
            <!--typeface 字体四种-->

    2、简单使用

      xml布局文件

            <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOn="打开"
                android:textOff="关闭"
                android:id="@+id/toggle"
                android:checked="true"
                android:disabledAlpha=".5"
                />
    
            <!--textOn,textOff  打开/关闭时文字-->
            <!--showText 是否显示文字-->
            <!--splitTrack 滑块是否和底部图片分隔-->
            <!--switchMinWidth 最小宽度-->
            <!--track 底部图片-->
            <!--thumb 滑块图片-->
            <!--typeface 字体四种-->
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOn="打开"
                android:textOff="关闭"
                android:showText="true"
                android:splitTrack="false"
                android:switchMinWidth="100dp"
                android:typeface="serif"
                android:id="@+id/switch11"
                />

      Java文件,点击事件处理:

            ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggle);
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b){
                        Log.d("toggle","打开了");
                    }else {
                        Log.d("switch","关闭了");
                    }
                }
            });
    
            final Switch switch1 = (Switch)findViewById(R.id.switch11);
            switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b){
                        Log.d("switch","开了");
                    }else {
                        Log.d("switch","关了");
    
                    }
                }
            });
  • 相关阅读:
    PHP设计模式
    PHP设计模式
    PHP 23种设计模式
    MySQL 中的共享锁和排他锁的用法
    PHP_MySQL高并发加锁事务处理
    Connection: close和Connection: keep-alive有什么区别
    罗辑思维首席架构师:Go微服务改造实践
    真诚与尊重是技术团队的管理要点
    10种常见的软件架构模式
    百亿级微信红包的高并发资金交易系统设计方案
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8081550.html
Copyright © 2011-2022 走看看