zoukankan      html  css  js  c++  java
  • 2.07ToggleButton(开关按钮)-Switch(开关)-UI组件-Android

    TOC

    ToggleButton(开关按钮)-Switch(开关)-UI组件-Android

    ToggleButton(开关按钮)

    属性名 说明
    android:disabledAlpha 设置按钮在禁用时的透明度
    android:textOff 按钮没有被选中时显示的文字
    android:textOn 按钮被选中时显示的文字 另外,除了这个我们还可以自己写个selector,然后设置下Background属性即可

    Switch(开关)

    属性名 说明
    android:showText 设置on/off的时候是否显示文字,boolean
    android:splitTrack 是否设置一个间隙,让滑块与底部图片分隔,boolean
    android:switchMinWidth 设置开关的最小宽度
    android:switchPadding 设置滑块内文字的间隔
    android:switchTextAppearance 设置开关的文字外观
    android:textOff 按钮没有被选中时显示的文字
    android:textOn 按钮被选中时显示的文字
    android:textStyle 文字风格,粗体,斜体写划线那些
    android:track 底部的图片
    android:thumb 滑块的图片
    android:typeface 设置字体,默认支持这三种:sans, serif, monospace;除此以外还可以使用 其他字体文件(*.ttf)

    案例

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color1"
        android:gravity="center"
        android:orientation="vertical">
    
    
        <ToggleButton
            android:id="@+id/tbtn_open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="关闭声音"
            android:textOn="打开声音" />
    
    
        <Switch
            android:id="@+id/swh_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:thumb="@drawable/thumb_selctor"
            android:track="@drawable/track_selctor" />
        <!--thumb设置滑块图片
            track是个设置底部椭圆形样式
        -->
    
    
    </LinearLayout>

    滑块

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@mipmap/switch_press_circle"/>
        <item android:state_pressed="false" android:drawable="@mipmap/switch_circle"/>
    </selector>

    底部

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@mipmap/switch_on_bg"/>
        <item android:state_checked="false" android:drawable="@mipmap/switch_off_bg"/>
    </selector>

    点击事件

    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.Switch;
    import android.widget.Toast;
    import android.widget.ToggleButton;
    
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    
    public class SwitchActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
        private ToggleButton tbtn_open;
        private Switch swh_status;
    
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.switch_layout);
            tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open);
            swh_status = (Switch) findViewById(R.id.swh_status);
            tbtn_open.setOnCheckedChangeListener(this);
            swh_status.setOnCheckedChangeListener(this);
        }
    
    
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            switch (compoundButton.getId()) {
                case R.id.tbtn_open:
                    if (compoundButton.isChecked()) {
                        Toast.makeText(this, "打开声音", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "关闭声音", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.swh_status:
                    if (compoundButton.isChecked()) {
                        Toast.makeText(this, "开关:ON", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "开关:OFF", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    }
  • 相关阅读:
    验证字符串空“” 的表达式
    should not this be a private conversation
    【转】你真的了解word-wrap 和 word-break 的区别吗
    To live is to function,that is all there is in living
    [转] windows 上用程序putty使用 ssh自动登录Linux(Ubuntu)
    Vim/gvim容易忘记的快捷键
    [转] Gvim for windows中块选择的方法
    问题集
    web services
    Trouble Shooting
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/1f5ac64ad77d7452cb773b7d83442a41.html
Copyright © 2011-2022 走看看