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

查看:178
本文介绍了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.

我的想象会是一个code,它看起来像:

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)

推荐答案

下面是我做的。不知道这是否会为你工作,但它的东西,你可以作为一个开始使用。 我的复选框,我不需要任何的检查监听器,但是你可以将它们添加到每个人,如果你想

Here is what I did. Not sure if this will work for you but its something you could use as a start. My check boxes I don't need any on check listeners, but you could add them to each one if you'd like

首先建立一个枚举作为检查paramater使用

first set up a enum to use as a check paramater

static enum CheckThis
{
  ONE, TWO, THREE
}

然后设置你的复选框,并创建一个方法,它枚举作为paramater并使用它在switch case语句。但在运行的开关case语句明确的复选框一切之前。说的记住,如果你有一个oncheked监听器上的复选框,这将执行里面的是,code。所以要么做一些只有onChecked或在我的情况下,它doesent问题,因为复选框只是有对视觉辅助。

then set up your check boxes and create a method that takes the enum as a paramater and uses it in a switch case statement. but before you run the switch case statement clear all of the check boxes. remeber that if you have a oncheked listener on the check box this will execute the code inside that that. so either do something only onChecked or in my case it doesent matter since the check boxes are only there for visual aid.

public void setCheckToCheckBoxGroup(CheckThis setCheck)
{
  checkBoxOne.setCheck(false);
  checkBoxTwo.setCheck(false);
  checkBoxThree.setChecked(false);
  switch(setCheck)
  {
   case ONE:
    checkBoxOne.setChecked(true);
    break;
   case TWO:
    checkBoxTwo.setChecked(true);
    break;
   case THREE:
    checkBoxThree.setChecked(true);
    break;
  }
}

现在只是用这种方法来设置检查的复选框,它会永远只能二选一。

now just use this method to set the check to the checkboxes and it will always only check one

希望这有助于你。

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

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