Question

I have 2 radio buttons and 4 check boxes that i want to keep count of. when you select radio button 1, you should be able to select 3 check boxes only. Following is my code. I tried the logic by Newts but it doesn't count correctly...Help!

        RadioButton radio3, radio6;
    int count=0;
    int pubsSelected=0;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          radio3 = (RadioButton) findViewById(R.id.radio0);
          radio6 = (RadioButton) findViewById(R.id.radio1);


        //---RadioButton1---
          radio3.setOnClickListener(new RadioGroup.OnClickListener() {
              public void onClick(View v){
                int  pubsSelected = 3;
                int  count=0;

DisplayToast("RadioButton is checked" +" "+ pubsSelected);  }
              });

        //---RadioButton2---
          radio6.setOnClickListener(new RadioGroup.OnClickListener() {
              public void onClick(View v){
                int  pubsSelected = 6;
                int  count=0;

DisplayToast("RadioButton is checked" + " "+ pubsSelected);  }
              });




          //---CheckBox 1---
            CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
            checkBox.setOnClickListener(new View.OnClickListener()
                    {
                public void onClick(View v) {
                    while(count < pubsSelected )    {
                    if (((CheckBox)v).isChecked()) 
                                    count++;
                    DisplayToast("CheckBox is checked" + " " + count);

                }
                }} );
          //---CheckBox 2---
            CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
            checkBox2.setOnClickListener(new View.OnClickListener()

            {
                public void onClick(View v) {
                    while(count < pubsSelected) {
                    if (((CheckBox)v).isChecked()) 
                        count++;
                    DisplayToast("CheckBox is checked" + " " + count);

                }
            }}); 
          //---CheckBox 3---

            CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
            checkBox3.setOnClickListener(new View.OnClickListener()

            {
                public void onClick(View v) {
                    while(count < pubsSelected) {
                }
                    if (((CheckBox)v).isChecked()) 
                        count++;
                    DisplayToast("CheckBox is checked" + " " + count);

                }
            });
//---CheckBox 4---

            CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
            checkBox4.setOnClickListener(new View.OnClickListener()

            {
                public void onClick(View v) {

                    if (pubsSelected==3){



                       DisplayToast( "you have selected" + pubsSelected );

                                        }
                    else{
                        if (pubsSelected==6){

                           DisplayToast(" you have selected" + pubsSelected );
                        }
                }}});
      }
            private int DisplayToast(String msg)
           {
                Toast.makeText(getBaseContext(), msg, 
                        Toast.LENGTH_SHORT).show();
                return count;  
           }
}
Was it helpful?

Solution

I think you have to put logic like this

UPDATED

on 4 check box check condition like this

if(pubsSelected==3)
{
uncheck
} 
else
{
checked
}

its another simple logic for you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top