Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

java - Android Check box Group

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
        }

    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...