Android checkbox实现多选一(不能不选)

先看效果图
在这里插入图片描述
xml代码如下

 <LinearLayout
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="50.0dip"
                android:orientation="horizontal"
                android:layout_gravity="end"
                >

                <CheckBox
                    android:id="@+id/checkbox_single1"
                    android:layout_width="0.0dip"
                    android:layout_height="@dimen/dimen_50dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8.0dip"
                    android:layout_marginRight="8.0dip"
                    android:layout_weight="1.0"
                    android:gravity="center"
                    android:text="单周"
                    android:textColor="@color/black"
                    android:textSize="15.0sp" />

                <View
                    android:layout_width="1.0px"
                    android:layout_height="fill_parent"
                    android:background="@color/black" />

                <CheckBox
                    android:id="@+id/checkbox_double1"
                    android:layout_width="0.0dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8.0dip"
                    android:layout_marginRight="8.0dip"
                    android:layout_weight="1.0"
                    android:gravity="center"
                    android:text="双周"
                    android:textColor="@color/black"
                    android:textSize="15.0sp" />

                <View
                    android:layout_width="1.0px"
                    android:layout_height="fill_parent"
                    android:background="@color/black" />

                <CheckBox
                    android:id="@+id/checkbox_all1"
                    android:layout_width="0.0dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8.0dip"
                    android:layout_marginRight="8.0dip"
                    android:layout_weight="1.0"
                    android:checked="true"
                    android:gravity="center"
                    android:text="全周"
                    android:textColor="@color/black"
                    android:textSize="15.0sp" />
            </LinearLayout>

以上定义了三个CheckBox:checkbox_single1(单周)、checkbox_double1(双周)、checkbox_all1(全周),且checkbox_all1(全周)默认选中。
java代码如下
设置监听

 		checkbox_all1.setOnClickListener(this);
        checkbox_single1.setOnClickListener(this);
        checkbox_double1.setOnClickListener(this);

在监听语句中

  case R.id.checkbox_all1:
                if (checkbox_all1.isChecked()) {
                    checkbox_double1.setChecked(false);
                    checkbox_single1.setChecked(false);
                }else {
                    checkbox_all1.setChecked(true);
                }
                break;
            case R.id.checkbox_double1:
                if (checkbox_double1.isChecked()) {
                    checkbox_all1.setChecked(false);
                    checkbox_single1.setChecked(false);
                }else {
                    checkbox_double1.setChecked(true);
                }
                break;
            case R.id.checkbox_single1:
                if (checkbox_single1.isChecked()) {
                    checkbox_all1.setChecked(false);
                    checkbox_double1.setChecked(false);
                } else {
                    checkbox_single1.setChecked(true);
                }
                break;
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值