How to disable a radio button without a group of radio stations?

I used the following code snippet to disable one radio button if another is selected, but when I launch the application and select both radio buttons, the code does not work, since both options remain selected.

I use a relative format, so I can’t use any other solutions with a group of radio, I just use two separate switches.

Can someone point out where I could be mistaken somewhere, since I do not see a lack of logic, any ideas?

In any case, this is the solution I'm trying to implement, but it does not work in the application:

public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radBtnMM:
                if (checked)
                    //set inch button to unchecked
                     radioInch.setChecked(false);
                break;
            case R.id.radBtnInch:
                if (checked)
                    //set MM button to unchecked
                    radioMM.setChecked(false);
                break;
        }
    }
+4
source share
3 answers

public void onRadioButtonClicked(View view) ? ?

:

layout.xml:

<RadioButton 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnMM"
/>
<RadioButton 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnInch"
/>

YourActivity.java:

public class MainActivity extends Activity implements OnCheckedChangeListener {
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    setContentView(R.layout.activity_main);
    rb1 = (RadioButton) findViewById(R.id.radBtnInch);
    rb1.setOnCheckedChangeListener(this);
    rb2 = (RadioButton) findViewById(R.id.radBtnMM);
    rb2.setOnCheckedChangeListener(this);
    return true;
  }
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      if (buttonView.getId() == R.id.radBtnInch) {
        rb2.setChecked(false);
      }
      if (buttonView.getId() == R.id.radBtnMM) {
        rb1.setChecked(false);
      }
    }
  }
+6

...

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch (view.getId()) {
    case R.id.radioBtnMM:
        // set inch button to unchecked
        radioBtnInch.setChecked(!checked);
        break;
    case R.id.radioBtnInch:
        // set MM button to unchecked
        radioBtnMM.setChecked(!checked);
        break;
    }
}
+1

:

@Override
public void onClick(View v)
{
    switch (v.getId()) 
    {
        case R.id.radBtnMM:
                if (checked)
                 {
                    radioInch.setChecked(false);
                 }
                 else
                 {
                   radioInch.setChecked(true);   
                 }
                break;
            case R.id.radBtnInch:
               if (checked)
                 {
                     radioMM.setChecked(false);
                 }
                 else
                 {
                    radioMM.setChecked(true);
                 }
                 break;
        }
    }
0

All Articles