回到第一个循环

时间:2017-07-12 19:07:03

标签: android loops continue

我想在同一活动中创建一个活动,其中包含“20张随机图像+每张图片下方4个按钮”(但另外)。 所以,我使用“ for loop ”来做到这一点。

现在,当用户点击其中一个按钮,然后创建一个图像和另一个随机按钮时,我想 回到循环的开头 应用

请指导我解决此问题。

感谢。

An image View with 4 button

代码:

注意:此代码无法正常工作,因为以下声明不起作用:

  

if (clk==true) continue;

public class testActivity extends Activity {
    int falseb;
boolean clk=false;
    String truename;
    final Random rnd = new Random();
   // public String[] picture={"banana","black","camel","cherry"};

   String [] picture = new String [] {"camel1","cat1","monkey","dogs" };

            List<String> List = new ArrayList<String>(Arrays.asList(picture));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        final ImageView img = (ImageView) findViewById(R.id.imgRandom);
        final Button b1 = (Button) findViewById(R.id.b1);
        final Button b2 = (Button) findViewById(R.id.b2);
        final Button b3 = (Button) findViewById(R.id.b3);
        final Button b4 = (Button) findViewById(R.id.b4);

        Typeface font = Typeface.createFromAsset(getAssets(), "koodk.ttf");
        b1.setTypeface(font);
        b2.setTypeface(font);
        b3.setTypeface(font);
        b4.setTypeface(font);
       for (int num=1;num<20;num++) {
           //set Image and 4 options(as Button)by random

           b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clk=true;

                    if (((Button) view).getText()==truename){
                        ((Button) view).setBackgroundResource(R.mipmap.true1);
                        List.remove(rp);

                    }else {
                        ((Button) view).setBackgroundResource(R.mipmap.fls1);
                        List.remove(rp);clk=true;
                    }

                }

            });


          b2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {clk=true;
                    if (((Button) view).getText()==truename){
                        ((Button) view).setBackgroundResource(R.mipmap.true1);
                        List.remove(rp);

                    }else {
                        ((Button) view).setBackgroundResource(R.mipmap.fls1);
                        List.remove(rp);
                    }
                }

            });
         if (clk==true) continue;
            b3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {clk=true;
                    if (((Button) view).getText()==truename){
                        ((Button) view).setBackgroundResource(R.mipmap.true1);
                        List.remove(rp);
                    }else {
                        ((Button) view).setBackgroundResource(R.mipmap.fls1);
                        List.remove(rp);
                    }
                }

            });
          if (clk==true) continue;
           b4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {clk=true;
                    if (((Button) view).getText()==truename){
                        ((Button) view).setBackgroundResource(R.mipmap.true1);
                        List.remove(rp);
                    }else {
                        ((Button) view).setBackgroundResource(R.mipmap.fls1);
                        List.remove(rp);
                    }
                }

            });
           if (clk==true) continue;

        }



   }



   protected final static int getResourceID
            (final String resName, final String resType, final Context ctx)
    {
        final int ResourceID =
                ctx.getResources().getIdentifier(resName, resType,
                        ctx.getApplicationInfo().packageName);
        if (ResourceID == 0)
        {
            throw new IllegalArgumentException
                    (
                            "No resource string found with name " + resName
                    );
        }
        else
        {
            return ResourceID;
        }
    }

}

1 个答案:

答案 0 :(得分:3)

Try to 

    for(int i = 0; i < 20; i++){
        if(response == false){
            //regenerate your list here
            i = 0;
        }
    }