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
java.lang.Object
↳ Android.view.View
↳ android.widget.TextView
↳android.widget.Button
↳android.widget.CompoundButton
↳android.widget.Switch
父类:compoundButton
Switch是一个可以再两种状态切换的开关控件。用户可以拖动来选择,也可以像选择复选框一样点击切换Switch的状态
在布局文件使用方法同TextView:
- <Switch
- android:id="@+id/switch_test"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
因为该组件继承自CompoundButton,在代码中可以通过实现CompoundButton.OnCheckedChangeListener接口,并实现其内部类的onCheckedChanged来监听状态变化。
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Switch switchTest = (Switch) findViewById(R.id.switch_test);
- switchTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- Toast.makeText(MainActivity.this, isChecked + "",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
在代码中也可以改变该组件的外观
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,需要自定义其属性,以下代码摘自网上:
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,需要自定义其属性,以下代码摘自网上:
- public class Switch extends CompoundButton {
- private static final int TOUCH_MODE_IDLE = 0;
- private static final int TOUCH_MODE_DOWN = 1;
- private static final int TOUCH_MODE_DRAGGING = 2;
- // Enum for the "typeface" XML parameter.
- private static final int SANS = 1;
- private static final int SERIF = 2;
- private static final int MONOSPACE = 3;
- private Drawable mThumbDrawable;
- private Drawable mTrackDrawable;
- private int mThumbTextPadding;
- private int mSwitchMinWidth;
- private int mSwitchPadding;
- private CharSequence mTextOn;
- private CharSequence mTextOff;
- private int mTouchMode;
- private int mTouchSlop;
- private float mTouchX;
- private float mTouchY;
- private VelocityTracker mVelocityTracker = VelocityTracker.obtain();
- private int mMinFlingVelocity;
- private float mThumbPosition;
- private int mSwitchWidth;
- private int mSwitchHeight;
- private int mThumbWidth; // Does not include padding
- private int mSwitchLeft;
- private int mSwitchTop;
- private int mSwitchRight;
- private int mSwitchBottom;
- private TextPaint mTextPaint;
- private ColorStateList mTextColors;
- private Layout mOnLayout;
- private Layout mOffLayout;
- private Context mContext;
- @SuppressWarnings("hiding")
- private final Rect mTempRect = new Rect();
- private static final int[] CHECKED_STATE_SET = {
- android.R.attr.state_checked
- };
- /**
- * Construct a new Switch with default styling.
- *
- * @param context The Context that will determine this widget's theming.
- */
- public Switch(Context context) {
- this(context, null);