1

Basically I have a Radio Group with two radio buttons, one of them is labelled RUN and the other is labelled PASS. Just underneath this I also have a check-box labelled "Pass Complete"

Question: How do I disable the check-box when RUN radio button is selected (so the checkbox can't be selected) and enable it while the PASS radio button is selected? Any help constructing some type of IF statement would be much appreciated.

XML

<RadioGroup
android:id="@+id/runandpass"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
androidrientation="horizontal">

<RadioButton
android:id="@+id/radiobuttonrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RUN" android:layout_weight="50"/>

<RadioButton
android:id="@+id/radiobuttonpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS" 
android:layout_weight="50"/>
</RadioGroup>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
androidrientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="" />

<CheckBox
android:id="@+id/checkBoxcmpltpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass Complete" 
android:layout_weight="6"/>
</LinearLayout>

JAVA

package com.aces.acesfootballuk;

import android.app.Activity;
import android.os.Bundle;

public class CoachesPage extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.coachespage);


}
};
silver_c
  • 61
  • 1
  • 1
  • 6

5 Answers5

4

I've managed to answer my question with a little more research, may help others as well...

private void initialize() {
    // TODO Auto-generated method stub
        RadioGroup1 = (RadioGroup) findViewById(R.id.runandpass);
        Radio1 = (RadioButton) findViewById(R.id.radiobuttonrun);
        Radio2 = (RadioButton) findViewById(R.id.radiobuttonpass);
        checkBoxcmpltpass = (CheckBox) findViewById(R.id.checkBoxcmpltpass);
        RadioGroup1.setOnCheckedChangeListener(this);
}


 public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    switch(checkedId){
    case R.id.radiobuttonrun:
        checkBoxcmpltpass.setEnabled(false);
    break;

    case R.id.radiobuttonpass:
        checkBoxcmpltpass.setEnabled(true);
    break;
    }
}
silver_c
  • 61
  • 1
  • 1
  • 6
2

try this :

        mRadioGroup1 = (RadioGroup) findViewById(R.id.runandpass);
        mRadio1 = (RadioButton) findViewById(R.id.radiobuttonrun);
        mRadio2 = (RadioButton) findViewById(R.id.radiobuttonpass);
        mcheckBoxcmpltpass= (RadioButton) findViewById(R.id.checkBoxcmpltpass);
        /*RadioGroup?OnCheckedChangeListener???*/
        mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
      }
      private RadioGroup.OnCheckedChangeListener mChangeRadio = new
               RadioGroup.OnCheckedChangeListener()
      {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
          // TODO Auto-generated method stub
          if(checkedId==mRadio1.getId())
          {
            /*?mRadio1*/
           mcheckBoxcmpltpass.disabled=false;
          }
          else if

(checkedId==mRadio2.getId())
      {
        /*?mRadio2*/
        mcheckBoxcmpltpass.disabled=true;
      }      
    }
  }; 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

try this

  final CheckBox cl_chk = (CheckBox) view.findViewById(R.id.cl_chk);
  cl_chk.setEnabled(false);

This will help you out...

1

Use Radiogroup.setOnCheckedChangeListener, then on checked change use checkbox.setenable

Alix Bloom
  • 217
  • 1
  • 8
1

You probably will want to add a listener to your checkbox, that will check for the selection and update the other views as needed.

 captureGrp = (RadioGroup) findViewById(R.id.runandpass);
 myRadio1 = (RadioButton) findViewById(R.drawable.radio1);
 myRadio2 = (RadioButton) findViewById(R.drawable.radio2);
 myRadio3 = (RadioButton) findViewById(R.drawable.radio3);

 captureGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

     public void onCheckedChanged(RadioGroup group, int checkedId) {


            if (checkedId == R.id.radio1) {
                //disable and enable as needed
            } else if (checkedId == R.id.radio2) {
                // disable as needed
            } else {
                // add as many as you need
            }
        }
Laith Alnagem
  • 654
  • 6
  • 13