Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在动态创建的复选框中选中_Java_Android_Checkbox - Fatal编程技术网

Java 在动态创建的复选框中选中

Java 在动态创建的复选框中选中,java,android,checkbox,Java,Android,Checkbox,我在运行时动态创建了一些复选框,现在我想知道其中一个是否被选中。我怎么能做到 for (int i = 0; i < cnt; i++) { cb = new CheckBox(getApplicationContext()); TextView txt = new TextView(getApplicationContext()); ll2 = new LinearLa

我在运行时动态创建了一些复选框,现在我想知道其中一个是否被选中。我怎么能做到

for (int i = 0; i < cnt; i++) {
                    cb = new CheckBox(getApplicationContext());
                    TextView txt = new TextView(getApplicationContext());
                    ll2 = new LinearLayout(
                            PollActivity.this);
                    ll2.setOrientation(LinearLayout.HORIZONTAL);

                    ll2.addView(cb);

                    ll.addView(ll2);

                }
for(int i=0;i
您需要设置为复选框,以便在单击复选框时触发事件。例如:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

      // do your code here
   }
});
我建议您为每个
复选框设置(在循环中)


现在您可以处理每个复选框的事件。

只需检查返回布尔值的cb.isChecked()。

给我们一些代码。需要看看它是如何创建的,但它不工作,你能给我一个完整的例子吗?我一天不能做这个,请帮帮我
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (isChecked) {
         // do some action
      }     
   }
});