CheckBox的简单使用

来源:互联网 发布:数据口径 编辑:程序博客网 时间:2024/05/19 22:54

CheckBox使我们平时比较常用的一个控件,它相当于是左边一个Button,右边一个TextView,它的功能使用不是很多,在这里我主要是介绍一下它在被按下、松开、选中、选中后松开的变化如何去实现。

首先我们先准备四张图片,图片我已经准备好了!接下来我们就开始实现一下吧!
新建layout_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!--相当于button + textView-->    <CheckBox        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="是否选中"        android:button="@drawable/box_selector"        /></LinearLayout>

drawable下面新建box_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:state_pressed="true" android:drawable="@mipmap/btn_check_on_pressed"></item>    <!--未选中,按下去-->    <item android:state_checked="false" android:state_pressed="true" android:drawable="@mipmap/btn_check_off_pressed"></item>    <!--选中的-->    <item android:state_checked="true" android:drawable="@mipmap/btn_check_on"></item>    <!--未选中-->    <item android:drawable="@mipmap/btn_check_off"></item></selector>

这样子,我们就实现了我们想要看到的效果,至于效果图显示,因为我是用手机调试,而不是使用模拟器,所以这里就展示了。读者可以自己更换图片,在自己的机子上面运行一下就可以看到效果啦!

0 0
原创粉丝点击