android初学-----SwitchButton

原始效果图预览android初学-----SwitchButton_第1张图片



正好现在没事做,就把之前看见的一个按钮写出来分享下吧, android里面并没有Switch 控件按钮,是从一个开源项目里面截取出来的 

有兴趣的同学下去自己研究吧。下载地址:https://github.com/Issacw0ng/SwitchButton

可以看见滑动的效果,而不是点击就关闭打开的效果  也不知道具体怎么截图效果图  就看代码吧。。


android初学-----SwitchButton_第2张图片


红色标记处为关键代码 

switchButton=(SwitchButton) findViewById(R.id.switchButton);
		switchButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if(arg1){
					Toast.makeText(getApplicationContext(), "打开", 1).show();
				}else{
					Toast.makeText(getApplicationContext(), "关闭", 1).show();
				}
			}
		});

从xml里面获取到swhtchButton  事件处理就和CheckBox 一样的。

<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"
    >
    
    <me.imid.view.SwitchButton
        android:id="@+id/switchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      />
</LinearLayout>

xml里面只要引用他就行了。

记得自己替换切换的图片或者样式  自己定义一下就好了 

效果图如下 android初学-----SwitchButton_第3张图片android初学-----SwitchButton_第4张图片




你可能感兴趣的:(android初学-----SwitchButton)