Android:Switch 语句 &图像按钮

发布于 2024-12-19 16:07:20 字数 5734 浏览 1 评论 0原文

我正在为 Android 编写一个记忆游戏,其中有 12 个由 ImageButtons 组成的“砖块”。 我一直在尝试使用 If 语句将 1 添加到计数器,如果按下看起来相同的两个按钮,则无法使其工作

所以我想我可以使用 Switch 语句来代替,但我不知道如何操作。 有人可以帮助我或至少为我指出正确的方向

编辑 到目前为止,我的代码只有前两个按钮 公共类 MemrlyActivity 扩展 Activity {

ImageButton img1,img2,img3,img4,img5,img6,img7,img8,img9,img10,img11,img12;
Button btnStart;
Button btnReset;
int bild1=0;
int bild2=0;
int bild3=0;
int bild4=0;
int bild5=0;
int bild6=0;
int bild7=0;
int bild8=0;
int bild9=0;
int bild10=0;
int bild11=0;
int bild12=0;
int test;
int result=0;

EditText count;

ArrayList<ImageButton>pic=new ArrayList<ImageButton>();


/** Called when the activity is first created. */

@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    img1=(ImageButton) findViewById(R.id.imageButton1);
    img2=(ImageButton) findViewById(R.id.imageButton2);
    img3=(ImageButton) findViewById(R.id.imageButton3);
    img4=(ImageButton) findViewById(R.id.imageButton4);
    img5=(ImageButton) findViewById(R.id.imageButton5);
    img6=(ImageButton) findViewById(R.id.imageButton6);
    img7=(ImageButton) findViewById(R.id.imageButton7);
    img8=(ImageButton) findViewById(R.id.imageButton8);
    img9=(ImageButton) findViewById(R.id.imageButton9);
    img10=(ImageButton) findViewById(R.id.imageButton10);
    img11=(ImageButton) findViewById(R.id.imageButton11);
    img12=(ImageButton) findViewById(R.id.imageButton12);
    btnStart=(Button) findViewById(R.id.buttonStart);
    btnReset=(Button) findViewById(R.id.buttonReset);
    count=(EditText) findViewById(R.id.editText1);
    pic.add(img1);
    pic.add(img2);
    pic.add(img3);
    pic.add(img4);
    pic.add(img5);
    pic.add(img6);
    pic.add(img7);
    pic.add(img8);
    pic.add(img9);
    pic.add(img10);
    pic.add(img11);
    pic.add(img12);



    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {

            Collections.shuffle(pic);
            pic.get(0).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(1).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(2).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(3).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(4).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(5).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(6).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(7).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(8).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(9).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(10).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));
            pic.get(11).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         for(int i=0;i<pic.size();i++){
                         pic.get(i).setImageDrawable(getResources().getDrawable(R.drawable.coin));
                         }
                         }
                    });
                }
            }).start();

     }
  });

img1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(pic.get(0)==img1){
                bild1=11;
            }else if(pic.get(1)==img1){
                bild1=11;
            }else if(pic.get(2)==img1){
                bild1=12;
            }else if(pic.get(3)==img1){
                bild1=12;
            }else if(pic.get(4)==img1){
                bild1=13;
            }else if(pic.get(5)==img1){
                bild1=13;
            }else if(pic.get(6)==img1){
                bild1=14;
            }else if(pic.get(7)==img1){
                bild1=14;
            }else if(pic.get(8)==img1){
                bild1=15;
            }else if(pic.get(9)==img1){
                bild1=15;
            }else if(pic.get(10)==img1){
                bild1=16;
            }else if(pic.get(11)==img1){
                bild1=16;
            }

        }
    });

img2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if(pic.get(0)==img2){
            bild2=11;
        }else if(pic.get(1)==img2){
            bild2=11;
        }else if(pic.get(2)==img2){
            bild2=12;
        }else if(pic.get(3)==img2){
            bild2=12;
        }else if(pic.get(4)==img2){
            bild2=13;
        }else if(pic.get(5)==img2){
            bild2=13;
        }else if(pic.get(6)==img2){
            bild2=14;
        }else if(pic.get(7)==img2){
            bild2=14;
        }else if(pic.get(8)==img2){
            bild2=15;
        }else if(pic.get(9)==img2){
            bild2=15;
        }else if(pic.get(10)==img2){
            bild2=16;
        }else if(pic.get(11)==img2){
            bild2=16;
        }

    }

}); 

/Fred

Im writing a memory game for android with 12 "bricks" that are made up by ImageButtons.
Ive been trying to use If statements to add 1 to a counter if the two buttons that look the same are pressed, cant get it to work

So i figured i could use a Switch statement to do this instead, but i have no idea how.
Could someone help or point me in the right direction atleast

Edit
My code so far, only with the two first buttons
public class MemrlyActivity extends Activity {

ImageButton img1,img2,img3,img4,img5,img6,img7,img8,img9,img10,img11,img12;
Button btnStart;
Button btnReset;
int bild1=0;
int bild2=0;
int bild3=0;
int bild4=0;
int bild5=0;
int bild6=0;
int bild7=0;
int bild8=0;
int bild9=0;
int bild10=0;
int bild11=0;
int bild12=0;
int test;
int result=0;

EditText count;

ArrayList<ImageButton>pic=new ArrayList<ImageButton>();


/** Called when the activity is first created. */

@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    img1=(ImageButton) findViewById(R.id.imageButton1);
    img2=(ImageButton) findViewById(R.id.imageButton2);
    img3=(ImageButton) findViewById(R.id.imageButton3);
    img4=(ImageButton) findViewById(R.id.imageButton4);
    img5=(ImageButton) findViewById(R.id.imageButton5);
    img6=(ImageButton) findViewById(R.id.imageButton6);
    img7=(ImageButton) findViewById(R.id.imageButton7);
    img8=(ImageButton) findViewById(R.id.imageButton8);
    img9=(ImageButton) findViewById(R.id.imageButton9);
    img10=(ImageButton) findViewById(R.id.imageButton10);
    img11=(ImageButton) findViewById(R.id.imageButton11);
    img12=(ImageButton) findViewById(R.id.imageButton12);
    btnStart=(Button) findViewById(R.id.buttonStart);
    btnReset=(Button) findViewById(R.id.buttonReset);
    count=(EditText) findViewById(R.id.editText1);
    pic.add(img1);
    pic.add(img2);
    pic.add(img3);
    pic.add(img4);
    pic.add(img5);
    pic.add(img6);
    pic.add(img7);
    pic.add(img8);
    pic.add(img9);
    pic.add(img10);
    pic.add(img11);
    pic.add(img12);



    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {

            Collections.shuffle(pic);
            pic.get(0).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(1).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(2).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(3).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(4).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(5).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(6).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(7).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(8).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(9).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(10).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));
            pic.get(11).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         for(int i=0;i<pic.size();i++){
                         pic.get(i).setImageDrawable(getResources().getDrawable(R.drawable.coin));
                         }
                         }
                    });
                }
            }).start();

     }
  });

img1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(pic.get(0)==img1){
                bild1=11;
            }else if(pic.get(1)==img1){
                bild1=11;
            }else if(pic.get(2)==img1){
                bild1=12;
            }else if(pic.get(3)==img1){
                bild1=12;
            }else if(pic.get(4)==img1){
                bild1=13;
            }else if(pic.get(5)==img1){
                bild1=13;
            }else if(pic.get(6)==img1){
                bild1=14;
            }else if(pic.get(7)==img1){
                bild1=14;
            }else if(pic.get(8)==img1){
                bild1=15;
            }else if(pic.get(9)==img1){
                bild1=15;
            }else if(pic.get(10)==img1){
                bild1=16;
            }else if(pic.get(11)==img1){
                bild1=16;
            }

        }
    });

img2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if(pic.get(0)==img2){
            bild2=11;
        }else if(pic.get(1)==img2){
            bild2=11;
        }else if(pic.get(2)==img2){
            bild2=12;
        }else if(pic.get(3)==img2){
            bild2=12;
        }else if(pic.get(4)==img2){
            bild2=13;
        }else if(pic.get(5)==img2){
            bild2=13;
        }else if(pic.get(6)==img2){
            bild2=14;
        }else if(pic.get(7)==img2){
            bild2=14;
        }else if(pic.get(8)==img2){
            bild2=15;
        }else if(pic.get(9)==img2){
            bild2=15;
        }else if(pic.get(10)==img2){
            bild2=16;
        }else if(pic.get(11)==img2){
            bild2=16;
        }

    }

}); 

/Fred

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文