سؤال

In my project I have two radio button under a radio group.By default the first radio button is selected.When first radio button is in selected state four checkboxes will be visible if second radio button is in selected state all four checkboxes will be gone to invisible state.I dont know how to implement this.Can anyone suggest me?

Thank you!!!

هل كانت مفيدة؟

المحلول

Its too simple. Earlier you mentioned that first time your first radio button is selected then at that time you have to just set visibility for checkboxes visible. Like

 ch1.setVisibility(View.VISIBLE); // same for other checkboxes

And when you select second radio button at that time set visibility for that check boxes GONE. Like

 ch1.setVisibility(View.GONE);   // same for other checkboxes

UPDATE:

 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if(checkedId == R.id.urfirstR1)
          {
                ch1.setVisibility(View.VISIBLE);

       else if(checkedId == R.id.ursecondr2)
          {
                ch1.setVisibility(View.GONE); 
          }
    }
});

نصائح أخرى

take this reference and do your work under this condition http://www.mkyong.com/android/android-radio-buttons-example/

take all check box under one layout and invisible or visible that layout according to this example

Try this

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

      if(checkedId == R.id.firstradiobutton)
               {
                 // visible all checkbox here
              }

       else if(checkedId == R.id.secondradiobutton)
              {
                  // invisible all checkbox here
              }
        }
    });

Try below solution.

By default you don't have to do anything. and onCheckchangelistener use below code.

radiobutton2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            ch1.setVisibility(View.INVISIBLE); 
           ch2.setVisibility(View.INVISIBLE); 
          ch3.setVisibility(View.INVISIBLE); 
        ch4.setVisibility(View.INVISIBLE); 
        }
    });

Put this inside your onCreate method. and replace ID name of checkboxes and radio button's as per your ID names.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top