Android 使用checkbox 做出switch开关效果和功能

来源:互联网 发布:淘宝中介发论文可靠吗 编辑:程序博客网 时间:2024/05/14 22:22

这里写图片描述

做到上面的功能,使用switch开关 自定义起来感觉麻烦,就使用checkbox自定义了一个,简单实用,点击上面任何位置都可以进行打开和关闭

xml中代码

    <CheckBox        android:id="@+id/checkbox_warning"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableRight="@drawable/checkbox_switch_selector"        android:button="@null"        android:text="报警"        android:layout_marginRight="90dp"        android:layout_marginLeft="70dp"        android:layout_marginTop="30dp"        android:textColor="@color/black"        android:textSize="35dp"         android:focusable="false" />

checkbox_switch_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_checked="true" android:drawable="@drawable/switch_on"/>    <item android:drawable="@drawable/switch_off"/></selector>

java文件中代码

        private CheckBox warningSwitch;        warningSwitch.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                 if(warningSwitch.isChecked()){                         ....                 }else{                         ....                 }            }        });
0 0
原创粉丝点击