0

I have a layout with 10 check boxes. All check boxes are added through code. Now I was wondering if there's any listener available for the layout to check how many check boxes are selected. For instance: when I select 4 check boxes, I want to know how many and which check boxes were selected.

4

2 回答 2

1

试试这个,取自这里

ckBox.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

      if (ckBox.isChecked()) {
        mDisplayHelp = true;
      } else {
        mDisplayHelp = false;
      }
      SharedPreferences.Editor editor = prefs.edit();
      editor.putBoolean("checkboxPref", mDisplayHelp);

      // Don't forget to commit your edits!!!
      editor.commit();
      // Optional part
    }
});

或者你可以像这样检查每一个:

checkBox = (CheckBox) findViewById(R.id.chkbox);
if (checkBox.isChecked()) {
    // Some wild things happen here
} else {
    // Okay ...
}
于 2013-07-03T08:55:12.637 回答
0

您可以添加复选框,就像CheckBox cb = new CheckBox(this); cb.setText("Dynamic Checkbox " + i); cb.setId(i+10); yourlayout.addView(cb);您必须通过调用 setOnClickListerner(this); 来设置他们的侦听器一样;这里。它将解决您的问题。

于 2013-07-03T09:01:36.850 回答