Butterknife: Proposal to add OnCheckedChanged for RadioGroup.

Created on 4 Jun 2014  Â·  51Comments  Â·  Source: JakeWharton/butterknife

Butterknife could probably also include OnCheckedChanged listener injection for RadioGroups.

Most helpful comment

@OnClick({ R.id.radio_pirates, R.id.radio_ninjas }) public void onRadioButtonClicked(RadioButton radioButton) {
    // Is the button now checked?
    boolean checked = radioButton.isChecked();

    // Check which radio button was clicked
    switch (radioButton.getId()) {
      case R.id.radio_pirates:
        if (checked) {
          // Pirates are the best
        }
        break;
      case R.id.radio_ninjas:
        if (checked) {
          // Ninjas rule
        }
        break;
    }
  }

All 51 comments

I want to use this in order to inject OnCheckedChangedListener into RadioGroup.

+1

+1 for this

+1

+1

+1 make it better

+1

+1

+1

+1

+1

Hi, what is the status of this? By trying to @Bind(R.id.radio_group_id) RadioGroup mMyRadioGroup;
I get a android.widget.RadioGroup cannot be cast to android.widget.CompoundButton.

@ticofab that is unrelated and sounds like you just need to clean your project.

Temporary workaround is to bind all radio buttons to the same callback and then check which is selected:

@OnClick({R.id.radio_button1, R.id.radio_button2})
public void radioGroupUpdate() {
    radio1.isChecked() ? ....
}

thanks @JakeWharton and @peterbartos for your comments!

+1

+1

+1

+1

+1

+1

+1

@OnClick({ R.id.radio_pirates, R.id.radio_ninjas }) public void onRadioButtonClicked(RadioButton radioButton) {
    // Is the button now checked?
    boolean checked = radioButton.isChecked();

    // Check which radio button was clicked
    switch (radioButton.getId()) {
      case R.id.radio_pirates:
        if (checked) {
          // Pirates are the best
        }
        break;
      case R.id.radio_ninjas:
        if (checked) {
          // Ninjas rule
        }
        break;
    }
  }

+1

+1

+1

So this listener is available?Now that this issue is closed?

@manideepl but the issue is not closed...

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+2

+1

+1

+1

+1

+1

+1

+1

This works

@OnCheckedChanged({R.id.rbt_yes, R.id.rbt_no})
public void radioButtonCheckChanged(CompoundButton v, boolean b){
     // insert your code here
}

+1

@BindView(R.id.cb_msg)
public CheckBox cbMsg;
@BindView(R.id.cb_phone)
public CheckBox cbPhone;
@OnCheckedChanged()
protected void onCheckedMode(CompoundButton compoundButton,boolean isChecked) {
if (compoundButton==cbMsg){
mode = isChecked?mode+ BlackList.MODE_SMS_INTERCEPT:mode-BlackList.MODE_SMS_INTERCEPT;
}
if (compoundButton==cbPhone){
mode = isChecked?mode+BlackList.MODE_PHONE_INTERCEPT:mode-BlackList.MODE_PHONE_INTERCEPT;
}
}
what the problem is? crashed message is com.android.internal.policy.impl.PhoneWindow$DecorView cannot be cast to android.widget.CompoundButton

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

voiddog picture voiddog  Â·  3Comments

anthony3444 picture anthony3444  Â·  3Comments

hwra2008 picture hwra2008  Â·  3Comments

engr-umar-qureshi picture engr-umar-qureshi  Â·  3Comments

ZakTaccardi picture ZakTaccardi  Â·  3Comments