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实现,抽取出来单独写一个例子结果如图:


布局文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="zhangphil.demo.MainActivity">  
  8.   
  9.     <com.kyleduo.switchbutton.SwitchButton  
  10.         android:id="@+id/switchButton"  
  11.         style="@style/SwitchButtonStyle"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_centerInParent="true"  
  15.         android:checked="false"  
  16.         app:kswAnimationDuration="300"  
  17.         app:kswBackDrawable="@drawable/ios_back_drawable"  
  18.         app:kswBackMeasureRatio="1.4"  
  19.         app:kswThumbDrawable="@drawable/ios_thumb_selector"  
  20.         app:kswThumbMarginBottom="-8dp"  
  21.         app:kswThumbMarginLeft="-5dp"  
  22.         app:kswThumbMarginRight="-5dp"  
  23.         app:kswThumbMarginTop="-2.5dp" />  
  24.   
  25. </RelativeLayout>  


上层Java代码:

[java]  view plain  copy
  1. package zhangphil.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.Toast;  
  7.   
  8. import com.kyleduo.switchbutton.SwitchButton;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         SwitchButton mSwitchButton = (SwitchButton) findViewById(R.id.switchButton);  
  18.         mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  19.             @Override  
  20.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {  
  21.                 String s;  
  22.                 if (b)  
  23.                     s = "选中";  
  24.                 else  
  25.                     s = "未选";  
  26.   
  27.                 Toast.makeText(getApplication(), s, Toast.LENGTH_SHORT).show();  
  28.             }  
  29.         });  
  30.     }  
  31. }  


这个例子已经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 

猜你喜欢

转载自blog.csdn.net/it666dhw/article/details/80613647