Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add check box counter android eclipse
    primarykey
    data
    text
    <p>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!</p> <pre><code> 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 &lt; 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 &lt; 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 &lt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload