Am a fresher in android..
am trying to design an app which contains a list of check boxes followed by radio group (contains 4 radio buttons each).
Concept is,until we select the particular checkbox its relevant radio group should be disabled.I tried all solutions provided at
How to disable a RadioGroup until checkbox is checked
Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first
Android disable radio group in xml
http://ift.tt/1GMVdXa but none of them are working for me..the app is crashing out..am providing my code here:
public class Service extends Activity { String op1=" ",op2=" "; CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1); CheckBox ck2 = (CheckBox)findViewById(R.id.checkBox2); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_service); RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1); RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2); rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio1: op1="Yes"; break; case R.id.radio2: op1="No"; break; } } });
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio3: op1="Yes"; break; case R.id.radio4: op1="No"; break; } } }); }
public void onCheckboxClicked(View view) { ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) { RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup4); for(int i = 0; i < rg1.getChildCount(); i++){ ((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked); } } }); for(int i = 0; i < rg1.getChildCount(); i++){ ((RadioGroup)rg1.getChildAt(i)).setEnabled(false); } } ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) { RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup5); for(int i = 0; i < rg2.getChildCount(); i++){ ((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked); } } }); for(int i = 0; i < rg2.getChildCount(); i++){ ((RadioGroup)rg2.getChildAt(i)).setEnabled(false); } }`
Aucun commentaire:
Enregistrer un commentaire