Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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,我正在尝试创建一个简单的android游戏,其中10个复选框随机出现在屏幕上,你必须尽快选中所有复选框。然后删除所有复选框。代码的第一部分起作用,创建了10个复选框。但如果我添加代码的第二部分,它根本不起作用,甚至不会出现复选框,应用程序也会冻结 公共作废复选框(视图v){ 复选框[]cb=新复选框[10] RelativeLayout r = (RelativeLayout) findViewById(R.id.layout); for (int i = 0; i < 1

我正在尝试创建一个简单的android游戏,其中10个复选框随机出现在屏幕上,你必须尽快选中所有复选框。然后删除所有复选框。代码的第一部分起作用,创建了10个复选框。但如果我添加代码的第二部分,它根本不起作用,甚至不会出现复选框,应用程序也会冻结

公共作废复选框(视图v){ 复选框[]cb=新复选框[10]

    RelativeLayout r = (RelativeLayout) findViewById(R.id.layout);
    for (int i = 0; i < 10; i++) {
        cb[i] = new CheckBox(this);
        CheckBox c =cb[i];
        int randy = (int) Math.floor(Math.random() * 1100 + 90);
        int randx = (int) Math.floor(Math.random() * 600);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(90, 90);
        params.leftMargin = randx;
        params.topMargin = randy;
        c.setId(i);
        r.addView(c, params);}  

    while (!checked(cb)){} //wait untill checked(cb)=true
    r.removeAllViews();

}
public boolean checked(CheckBox[] cb) { //returns true if all checkboxes are checked
boolean b = true;
    for (CheckBox c:cb)
    {
      if (!c.isChecked()){
          b=false;
      }
    }


    return b;

}
RelativeLayout r=(RelativeLayout)findViewById(r.id.layout);
对于(int i=0;i<10;i++){
cb[i]=新复选框(此复选框);
复选框c=cb[i];
int randy=(int)Math.floor(Math.random()*1100+90);
int randx=(int)Math.floor(Math.random()*600);
RelativeLayout.LayoutParams参数=新的RelativeLayout.LayoutParams(90,90);
params.leftMargin=randx;
参数topMargin=randy;
c、 setId(i);
r、 addView(c,params);}
while(!checked(cb)){}//wait untill checked(cb)=true
r、 移除所有视图();
}
选中公共布尔值(复选框[]cb){//如果选中所有复选框,则返回true
布尔b=真;
用于(复选框c:cb)
{
如果(!c.isChecked()){
b=假;
}
}
返回b;
}
编辑代码:

int CheckedCount = 0;
int j = 0;
CheckBox[] cb = new CheckBox[10];
Boolean[] checked = new Boolean[10];

public void checkbox(View v) {
    Arrays.fill(checked, Boolean.FALSE);
    RelativeLayout r = (RelativeLayout) findViewById(R.id.layout);
    for ( j = 0; j < 10; j++) {
        cb[j] = new CheckBox(this);
        CheckBox c =cb[j];
        int randy = (int) Math.floor(Math.random() * 1100 + 90);
        int randx = (int) Math.floor(Math.random() * 600);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(90, 90);
        params.leftMargin = randx;
        params.topMargin = randy;
        c.setId(j); //id set for each checkbox
        c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //missing code

                boolean c =true;
                for (boolean d:checked){
                    if (!d){
                        c=false;
                    }
                }

                if (c) {
                    RelativeLayout r = (RelativeLayout) findViewById(R.id.layout);
                    r.removeAllViews();
                }
    /*           //old code
                CheckedCount++;
                if (CheckedCount == 10) {
             RelativeLayout r = (RelativeLayout) findViewById(R.id.layout);
                    for (int a= 0; a<10;a++){
                        CheckBox c = cb[i];
                        r.removeView(c);
                    }
                    r.removeAllViews();
                }*/
            }
        });
        r.addView(c, params);

    }

}}
int CheckedCount=0;
int j=0;
复选框[]cb=新复选框[10];
布尔值[]选中=新布尔值[10];
公共作废复选框(视图v){
数组.fill(选中,布尔值.FALSE);
RelativeLayout r=(RelativeLayout)findViewById(r.id.layout);
对于(j=0;j<10;j++){
cb[j]=新复选框(该复选框);
复选框c=cb[j];
int randy=(int)Math.floor(Math.random()*1100+90);
int randx=(int)Math.floor(Math.random()*600);
RelativeLayout.LayoutParams参数=新的RelativeLayout.LayoutParams(90,90);
params.leftMargin=randx;
参数topMargin=randy;
c、 setId(j);//为每个复选框设置id
c、 setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮复合按钮,布尔b){
//缺失代码
布尔c=真;
for(布尔值d:选中){
如果(!d){
c=假;
}
}
如果(c){
RelativeLayout r=(RelativeLayout)findViewById(r.id.layout);
r、 移除所有视图();
}
/*//旧代码
CheckedCount++;
如果(CheckedCount==10){
RelativeLayout r=(RelativeLayout)findViewById(r.id.layout);

对于(int a=0;a使用while循环不是确定UI中更改的正确方法。相反,我建议您为每个复选框的
onCheckedChanged
事件添加一个侦听器,然后才检查复选框的状态(使用“checked”等方法).

它可以工作,但只有一个侦听器。我如何实现onCheckedChanged侦听器来为每个复选框生成侦听器?您应该将其添加到方法“checkbox”的循环中,并在侦听器中调用“checked”。我在上面添加了编辑过的代码。首先,我尝试在每次选中复选框时递增计数(标记为//旧代码)。它起作用了,但问题是我可以只选中和取消选中一个复选框10次。为了解决这个问题,我做了一个假布尔数组。每次单击一个复选框时,数组中的一个布尔值将与复选框的索引设置为true。但问题是,我不知道如何获取在li中选中的复选框的索引stener方法。你没有太多。事实上,在我看来,你的检查方法应该检查每个复选框的状态,不管它是否被点击。这比有活动变量来指示状态容易得多。每次点击都要检查它们。