简体   繁体   中英

RadioButton-CheckBox Combo

For my particular application, I want to be able to toggle a radiobutton on and off by touching it, similar to a checkbox. However, I need the radiobuttons to be mutually exclusive within their groups. There are 64 buttons and 8 groups, so I don't want to use checkboxes and program the toggle functionality manually. Can I use an onTouchListener() with a radiobutton? Is there any way to accomplish this? Thank you in advance for any support.

You are looking for a UI paradigm that is neither a checkbox nor a radio button. Your best bet is to use check boxes and then implement the mutual exclusivity on top of that. eg,

myCheckbox1.setOnCheckChangedListener(new OnCheckChangedListener() {
  @Override
  public void onCheckChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      myCheckbox2.setChecked(false);
      myCheckbox3.setChecked(false);
      myCheckbox4.setChecked(false);

      // start playback
    } else {
      // stop playback
    }
  } 
);

Note that if you are able to enumerate all of the button IDs per group, you can write the code generically, like,

    if (isChecked) {
      int myGroup = (Integer) buttonView.getTag(); // Define the group in the view's tag
      for (int id: groups.get(myGroup)) {
        if (id != buttonView.getId()) ((CompoundButton) findViewById(id)).setChecked(false); 
    }

The other, more complicated answer would be to build your own UI widget and give it whatever semantics you want. That's not scope of what I could describe here though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM