Android studio学习之——CheckBox控件复选框 多选按钮的使用

CheckBox的常用属性
这里使用的是自定义选项前面的的选择框 android:button="@drawable/bg_checkbox",详情请看上一篇文章中的自定义添加背景图片。

<CheckBox
    android:id="@+id/cb_5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="暴富"
    android:button="@drawable/bg_checkbox"
    android:textSize="20sp" />

<CheckBox
    android:id="@+id/cb_6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="暴瘦"
    android:button="@drawable/bg_checkbox"
    android:textSize="20sp" />

<CheckBox
    android:id="@+id/cb_7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="长高"
    android:button="@drawable/bg_checkbox"
    android:textSize="20sp" />

<CheckBox
    android:id="@+id/cb_8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="变白"
    android:button="@drawable/bg_checkbox"
    android:textSize="20sp" />

在java文件中 编写如下

先声明控件
mCb5=(CheckBox)findViewById(R.id.cb_5);
mCb6=(CheckBox)findViewById(R.id.cb_6);
mCb7=(CheckBox)findViewById(R.id.cb_7);
mCb8=(CheckBox)findViewById(R.id.cb_8);

mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override//设置状态发生变化的监听事件
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Toast.makeText(CheckBoxActivity.this,b?"哈哈有钱了":"想多了",Toast.LENGTH_SHORT).show();
    }
});
mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override//设置状态发生变化的监听事件
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Toast.makeText(CheckBoxActivity.this,b?"你又瘦了":"别吃了胖死得了",Toast.LENGTH_SHORT).show();
    }
});
mCb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override//设置状态发生变化的监听事件
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Toast.makeText(CheckBoxActivity.this,b?"看我一米八的大长腿":"呵呵,小矮子",Toast.LENGTH_SHORT).show();
    }
});
mCb8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override//设置状态发生变化的监听事件
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Toast.makeText(CheckBoxActivity.this,b?"一白遮白丑":"怕是白痴吧",Toast.LENGTH_SHORT).show();
    }
});

在资源文件之下新建Drawable Resource file文件,命名为bg_checkbox,根布局为selector 的这样的一个背景选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item
       android:state_checked="false"    <--不选中时-->
       android:drawable="@drawable/lala_false"<--自定义添加的图片-->
       />
    <item
        android:state_checked="true"   <--选中时-->
        android:drawable="@drawable/haha_true"<--自定义添加的图片-->	        />
</selector>

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值