zoukankan      html  css  js  c++  java
  • 每日总结

    今天对开关按钮ToggleButton进行实例操作

    为Switch设置下滑块和底部的图片,实现 一个类似于IOS 7的滑块的效果,但是有个缺点就是不能在XML中对滑块和底部的大小进行设置, 就是素材多大,Switch就会多大,我们可以在Java中获得Drawable对象,然后对大小进行修改, 简单的例子:

    运行效果图:

    实现代码: 先是两个drawable的文件: thumb_selctor.xml:

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

    track_selctor.xml:

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

    布局文件:activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <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:textOff=""
            android:textOn=""
            android:thumb="@drawable/thumb_selctor"
            android:track="@drawable/track_selctor" />
    
    </LinearLayout>

    MainActivity.java:

    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    
        private ToggleButton tbtn_open;
        private Switch swh_status;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            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;
    
            }
        }
    }
  • 相关阅读:
    Swift流程控制之循环语句和判断语句详解
    框架内的文件集合
    十分钟让你明白Objective-C的语法(和Java、C++的对比)
    Swift版音乐播放器(简化版),swift音乐播放器
    通过数字电视通过宽带网络取代互联网电视机顶盒应用
    JS学习笔记-OO创建怀疑的对象
    如果不能显示真正的考验个别车型toast问题解决
    swift 它们的定义TabBarItem
    NSUserDefaults API简单的介绍和使用英文文件
    FZU 1686 龙之谜 重复覆盖
  • 原文地址:https://www.cnblogs.com/lxywsx/p/14910357.html
Copyright © 2011-2022 走看看