Android 复选框组 [英] Android Check box Group

查看:50
本文介绍了Android 复选框组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一组复选框应用某种验证(例如,两个相互矛盾的项目不能一起检查)所以我想以某种方式对 Check Box 对象进行分组,并在整个组中应用类似 RequiredFieldValidator 的东西该验证器我将注册侦听器并对我的 Check Boxes 对象进行全面检查.

I'm trying to apply some kind of validation on a group of check boxes (e.g. Two contradictory items cannot be checked together) so I want to somehow group Check Box objects and apply something like RequiredFieldValidator on the whole group once and in that validator I will register listeners and do the whole check on my Check Boxes objects.

我想象的代码应该是这样的:

What I imagine would be a code that look like that:

CheckBoxView allMyCheckBoxes = new CheckBoxView(checkbox1,checkbox2,checkbox3); //varargs
validate(allMyCheckBoxes);

Validate 将包含矛盾的复选框和所有内容的逻辑.

Validate will contain the logic of contradictory check boxes and everything.

这是否已经在 Android 的某个地方实现了?如果没有,有人尝试过类似的东西吗?(希望在这里与我们分享)

Is that already implemented somewhere in Android? If not, anybody tried out something like that? (Hopefully share it with us here)

推荐答案

此逻辑将允许选择一个和多个复选框

This logic will allow to select one and more checkbox

 private Checkbox day = (checkbox)findviewbyid(R.id.day);
 private Checkbox night =(checkbox)findviewbyid(R.id.night);

 day.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!night.isChecked() && !day.isChecked()) {
            night.setChecked(true);
        }

    }
});
night.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!day.isChecked() && !night.isChecked()) {
            day.setChecked(true);
        }

    }
});

这篇关于Android 复选框组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆