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

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

问题描述

我想对一组复选框应用某种验证(例如两个矛盾的项目不能一起检查)所以我想以某种方式组合复选框对象,并应用像RequiredFieldValidator一样整个组一次和

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 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()) {

                } else {
                    night.setChecked(true);
                }

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

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

            }
        });

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

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