简体   繁体   中英

How to check how many checkboxes are checked and display in textview on button clicked

I have 3 check boxes and textviews, I want to keep track how many checkboxes are checked and which and display final result in textView on button clicked. How can I do that?

Let say you have a parent LinearLayout and and it has three checkboxes.

Create reference of LinearLayout.

LineaLayout linearLayout = (LinearLayout) findVIewById(R.id.lv);

Then, CheckBox states you have to iterate through ChildViews of linearlayout.

Something like,

for (int i = 0; i < linearLayout.getChildCount(); i++) {
    View v = linearLayout.getChildAt(i);
    if (v instanceof CheckBox) {
      if (((CheckBox) v).isChecked())
      // Check Checkbox
      else
      // Unchecked Checkbox
    } 
}

To determine if a CheckBox is checked, you can call myCheckbox.isChecked() . To set the value of a TextView , you can call myTextView.setText() . And to do something when a Button is pressed, you can add a View.OnClickListener using myButton.setOnClickListener() .

Together, that means you can create a program like this:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        final TextView textView = (TextView) findViewById(R.id.text);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = 0;

                if (checkBox1.isChecked()) {
                    ++count;
                }

                if (checkBox2.isChecked()) {
                    ++count;
                }

                if (checkBox3.isChecked()) {
                    ++count;
                }

                textView.setText("How many checked? " + count);
            }
        });
    }
}

The best approach is to use listview :

Create a ListView first and create a custom adapter having the the checkbox in each rows.

In Adapter have a Set<Integer> indexes = new HashSet<Integer>()

In the getView() method : getView (int position, View convertView, ViewGroup parent)

Assign click listener for the checkbox :

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) 
       {
          if(isChecked){
             indexes.add(position); 
          }else{
             indexes.remove(position);
          }

       }
   }
); 

Finally just access the set and you will get, length of set, ie the number of checkboxes selected and the values represent the position of the selected checkbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM