正好现在没事做,就把之前看见的一个按钮写出来分享下吧, 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(); } } });
<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>
记得自己替换切换的图片或者样式 自己定义一下就好了