现在的位置: 首页 > 综合 > 正文

Android Switch控件介绍 Android Switch控件介绍

2013年08月16日 ⁄ 综合 ⁄ 共 3625字 ⁄ 字号 评论关闭
 

Android Switch控件介绍

分类: Android 2318人阅读 评论(1) 收藏 举报
IOS有一种UISwitch控件,只有两个状态:on,off。如图所示


在Android4.0中也添加了一个类似的控件:Switch.如图所示

 
其类关系图如下:
java.lang.Object
   ↳ Android.view.View
    ↳ android.widget.TextView
    ↳android.widget.Button
    ↳android.widget.CompoundButton
    ↳android.widget.Switch
父类:compoundButton
Switch是一个可以再两种状态切换的开关控件。用户可以拖动来选择,也可以像选择复选框一样点击切换Switch的状态
在布局文件使用方法同TextView:

[java] view
plain
copy

  1. <Switch  
  2.         android:id="@+id/switch_test"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="match_parent" />  

因为该组件继承自CompoundButton,在代码中可以通过实现CompoundButton.OnCheckedChangeListener接口,并实现其内部类的onCheckedChanged来监听状态变化。

[java] view
plain
copy

  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         Switch switchTest = (Switch) findViewById(R.id.switch_test);  
  8.         switchTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  9.                     @Override  
  10.                     public void onCheckedChanged(CompoundButton buttonView,  
  11.                             boolean isChecked) {  
  12.                         Toast.makeText(MainActivity.this, isChecked + "",  
  13.                                 Toast.LENGTH_SHORT).show();  
  14.                     }  
  15.                 });  
  16.     }  
  17. }  

在代码中也可以改变该组件的外观

setSwitchTextAppearance(Context context, int resid) 使用指定的资源id设置状态标签上的文字大小,类型,颜色等;
setSwitchTypeface(Typeface tf, int style)  使用指定的字体类型库内的指定类型来设置状态标签上的文字;
setSwitchTypeface(Typeface tf) 使用指定字体类型库内的固有类型来设置状态标签上的文字;
setTextOff(CharSequence textOff) 设置“关闭”状态标签文字;
setTextOn(CharSequence textOn) 设置“开启”状体标签文字;
父类内的setButtonDrawable(int resid) 用指定的资源id设置组件背景;
父类内的setButtonDrawable(Drawable d) 用可绘制对象设置组件背景;
android:textStyle 和android:typeface 与setSwitchTypeface(Typeface tf)对应;
如果想在2.2中使用Switch,需要自定义其属性,以下代码摘自网上:

[java] view
plain
copy

  1. public class Switch extends CompoundButton {  
  2.     private static final int TOUCH_MODE_IDLE = 0;  
  3.     private static final int TOUCH_MODE_DOWN = 1;  
  4.     private static final int TOUCH_MODE_DRAGGING = 2;  
  5.   
  6.     // Enum for the "typeface" XML parameter.  
  7.     private static final int SANS = 1;  
  8.     private static final int SERIF = 2;  
  9.     private static final int MONOSPACE = 3;  
  10.   
  11.     private Drawable mThumbDrawable;  
  12.     private Drawable mTrackDrawable;  
  13.     private int mThumbTextPadding;  
  14.     private int mSwitchMinWidth;  
  15.     private int mSwitchPadding;  
  16.     private CharSequence mTextOn;  
  17.     private CharSequence mTextOff;  
  18.   
  19.     private int mTouchMode;  
  20.     private int mTouchSlop;  
  21.     private float mTouchX;  
  22.     private float mTouchY;  
  23.     private VelocityTracker mVelocityTracker = VelocityTracker.obtain();  
  24.     private int mMinFlingVelocity;  
  25.   
  26.     private float mThumbPosition;  
  27.     private int mSwitchWidth;  
  28.     private int mSwitchHeight;  
  29.     private int mThumbWidth; // Does not include padding  
  30.   
  31.     private int mSwitchLeft;  
  32.     private int mSwitchTop;  
  33.     private int mSwitchRight;  
  34.     private int mSwitchBottom;  
  35.   
  36.     private TextPaint mTextPaint;  
  37.     private ColorStateList mTextColors;  
  38.     private Layout mOnLayout;  
  39.     private Layout mOffLayout;  
  40.   
  41.     private Context mContext;  
  42.   
  43.     @SuppressWarnings("hiding")  
  44.     private final Rect mTempRect = new Rect();  
  45.   
  46.     private static final int[] CHECKED_STATE_SET = {  
  47.         android.R.attr.state_checked  
  48.     };  
  49.   
  50.     /** 
  51.      * Construct a new Switch with default styling. 
  52.      * 
  53.      * @param context The Context that will determine this widget's theming. 
  54.      */  
  55.     public Switch(Context context) {  
  56.         this(context, null);  

抱歉!评论已关闭.