您的位置:首页 > 移动开发 > Android开发

android初学-----SwitchButton

2014-02-20 14:36 197 查看
原始效果图预览


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

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

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



红色标记处为关键代码

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里面只要引用他就行了。

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

效果图如下


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: