zoukankan      html  css  js  c++  java
  • Android第三方开源SwitchButton


    Android第三方开源SwitchButton

    Android SwitchButton是github上的一个第三方开源项目,其项目主页是:https://github.com/kyleduo/SwitchButton
    Android平台上的Switch Button样式单一,SwitchButton旨在丰富Android平台的Switch样式的Button,其实现的结果如图:



    注意到SwitchButton其中一个实现,就是iOS样式的Switch切换开关。
    SwitchButton本身给出实例代码结构不是很简单直观,我把其中关于iOS的SwitchButton实现,抽取出来单独写一个例子结果如图:


    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="zhangphil.demo.MainActivity">
    
        <com.kyleduo.switchbutton.SwitchButton
            android:id="@+id/switchButton"
            style="@style/SwitchButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:checked="false"
            app:kswAnimationDuration="300"
            app:kswBackDrawable="@drawable/ios_back_drawable"
            app:kswBackMeasureRatio="1.4"
            app:kswThumbDrawable="@drawable/ios_thumb_selector"
            app:kswThumbMarginBottom="-8dp"
            app:kswThumbMarginLeft="-5dp"
            app:kswThumbMarginRight="-5dp"
            app:kswThumbMarginTop="-2.5dp" />
    
    </RelativeLayout>


    上层Java代码:

    package zhangphil.demo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.Toast;
    
    import com.kyleduo.switchbutton.SwitchButton;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            SwitchButton mSwitchButton = (SwitchButton) findViewById(R.id.switchButton);
            mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    String s;
                    if (b)
                        s = "选中";
                    else
                        s = "未选";
    
                    Toast.makeText(getApplication(), s, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    



    我写的这个例子已经push到github上,该代码的项目主页是:https://github.com/zhangphil/Android_iOS_SwitchButton


    附录文章:
    1,《Android Segmented RadioButton》链接地址:http://blog.csdn.net/zhangphil/article/details/51441677 
    2,《Android选项切换条SHSegmentControl》链接地址:http://blog.csdn.net/zhangphil/article/details/49720805
    3,《Android ToggleButton:状态切换的Button》链接地址:http://blog.csdn.net/zhangphil/article/details/51720565

  • 相关阅读:
    上传base64编码图片
    mysql
    当程序员老去 揭秘不为人知背后的辛酸故事
    Java中break的作用
    Random类
    使用dsoframer控件出现"Unable to display the inactive document. Click here to reactivate the document."的问题
    给IT新人的15个建议:苦逼程序员的辛酸反省与总结
    XML开发总结
    解决Office软件冲突问题
    PopupMenu控件的使用
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6147302.html
Copyright © 2011-2022 走看看