3

我已经用谷歌搜索了这个问题,但找不到一个简单或足够具体的答案:任何有经验的程序员可以帮助我......?

我在一个名为 dialog1 的自定义对话框中有一组 10 个复选框。

public class DigitsActivity extends Activity {
...
CheckBox[] ckbDigits = new CheckBox[10];
...


public void SelectDigit1(){  // method which opens the custom dialog

Context context=DigitsActivity.this;
final Dialog dialog1 = new Dialog(context);
...
for (int k = 0; k <= 9; k++){
  ckbDigits[k] = (CheckBox) dialog1.findViewById(R.id.ckbDigits[k]);
}
...
}

我在生产线上遇到多个错误

ckbDigits[k] = ...

我也试过:

ckbDigits[] = (CheckBox) dialog1.findViewById(R.id.ckbDigits[]);

但它也不起作用......

谁能告诉我如何声明一组复选框?

谢谢。

4

3 回答 3

1

宣布 CheckBox chk[];

然后这样做

  chk=new CheckBox[size];
  for (int i = 0; i < size; i++) {
            chk[i] = (CheckBox) dialog.findViewById(R.id.checkBox);
            chk[i].setChecked(true);
           // whatever operations
        }
于 2016-06-18T17:51:26.057 回答
0
  1. have you called "setContentView" before trying to find the views?
  2. have you initialized the array of ids ?
  3. what's this : "R.id.ckbDigits[]" ?
  4. in any case , i would suggest naming the ids as something like "checkbox0" , "checkbox1" ,... , and then find them by using getIdentifier (like this) , in order to make the initialization of the ids easier . you could make it stop when it didn't find the id that it has searched for.
于 2012-05-25T23:35:44.320 回答
-1

您必须像这样声明 CheckBox 的新向量

例如 :

public class Test{
   static int ckbDigits[]={id.ckbDigits0,id.ckbDigits1,id.ckbDigits2,id.ckbDigits3,
    id.ckbDigits4
    id.ckbDigits5,id.ckbDigits6,id.ckbDigits7,id.ckbDigits8,id.ckbDigits9};

}

在你的代码中做了这样的事情之后

for (int k = 0; k <= 9; k++){
  ckbDigits[k] = (CheckBox) dialog1.findViewById(Test.ckbDigits[k]);
}
于 2013-05-25T12:48:33.320 回答