我该如何写“如果按钮被点击"?在android studio中的If语句中? [英] How can I write "if button clicked" in an If statement in android studio?

查看:165
本文介绍了我该如何写“如果按钮被点击"?在android studio中的If语句中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用四张纸牌(2x2)构建一个记忆游戏.这四个卡具有名为卡"的onClick.此onClick包含一个If语句,如果语句不相同,则将其向后翻转,如果相同,则保留它们.

I am building a memory game with four cards(2x2). These four cards have an onClick named "cards". This onClick consists of an If statement that flips the cards back if they are not the same, and keeps them if they are the same.

4张卡片的正面图像相同,但背面图像不同.我的问题是我想翻转卡片,但它们已经具有onClick功能.那么,如何在If语句中编写如果单击了if按钮",或者还有其他解决方案?

The front image of the card is the same for the 4, but the back has different images.My problem is that I want the cards to flip, but they already have an onClick. So how can I write "if button clicked" in an If statement or is there another solution?

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.INVISIBLE);
            pic1 = (ImageView) findViewById(R.id.imageView);
            pic2 = (ImageView) findViewById(R.id.imageView2);
            pic3 = (ImageView) findViewById(R.id.imageView3);
            pic4 = (ImageView) findViewById(R.id.imageView4);

            pic1.setImageResource(R.drawable.img1);
            pic2.setImageResource(R.drawable.img1);
            pic3.setImageResource(R.drawable.img1);
            pic4.setImageResource(R.drawable.img1);

            pic1.setVisibility(View.VISIBLE);
            pic2.setVisibility(View.VISIBLE);
            pic3.setVisibility(View.VISIBLE);
            pic4.setVisibility(View.VISIBLE);


        }
    });

}


public void cards(View v) {


    if (v.getId() == pic1.getId() ) {
        pic1.setImageResource(R.drawable.img2);
        pic1.setTag("img2");
    } else if (v.getId() == pic2.getId()) {
        pic2.setImageResource(R.drawable.img2);
        pic2.setTag("img2");
    } else if (v.getId() == pic3.getId()) {
        pic3.setImageResource(R.drawable.img3);
        pic3.setTag("img3");
    } else if (v.getId() == pic4.getId()) {
        pic4.setImageResource(R.drawable.img3);
        pic4.setTag("img3");
    }

    if (R.drawable.img2 == R.drawable.img2) {
        pic1.setImageResource(R.drawable.img2);
        pic1.getTag();
        pic2.setImageResource(R.drawable.img2);
        pic2.getTag();
    }
    if (R.drawable.img3 == R.drawable.img3) {
        pic3.setImageResource(R.drawable.img3);
        pic3.getTag();
        pic4.setImageResource(R.drawable.img3);
        pic4.getTag();

    }
    if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
        pic1.setImageResource(R.drawable.img1);
        pic2.setImageResource(R.drawable.img1);
        pic3.setImageResource(R.drawable.img1);
        pic4.setImageResource(R.drawable.img1);


    }


}

我尝试的第二种方法:@Overridepublic void onClick(View v){button1.setVisibility(View.INVISIBLE);pic1 =(ImageView)findViewById(R.id.imageView);pic2 =(ImageView)findViewById(R.id.imageView2);pic3 =(ImageView)findViewById(R.id.imageView3);pic4 =(ImageView)findViewById(R.id.imageView4);

SECOND METHOD I'M TRYING: @Override public void onClick(View v) { button1.setVisibility(View.INVISIBLE); pic1 = (ImageView) findViewById(R.id.imageView); pic2 = (ImageView) findViewById(R.id.imageView2); pic3 = (ImageView) findViewById(R.id.imageView3); pic4 = (ImageView) findViewById(R.id.imageView4);

            pic1.setImageResource(R.drawable.img1);
            pic2.setImageResource(R.drawable.img1);
            pic3.setImageResource(R.drawable.img1);
            pic4.setImageResource(R.drawable.img1);

            pic1.setVisibility(View.VISIBLE);
            pic2.setVisibility(View.VISIBLE);
            pic3.setVisibility(View.VISIBLE);
            pic4.setVisibility(View.VISIBLE);


            if (R.drawable.img2 == R.drawable.img2) {
                pic1.setImageResource(R.drawable.img2);

                pic2.setImageResource(R.drawable.img2);

            }
            if (R.drawable.img3 == R.drawable.img3) {
                pic3.setImageResource(R.drawable.img3);

                pic4.setImageResource(R.drawable.img3);


            }
            if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
                pic1.setImageResource(R.drawable.img1);
                pic2.setImageResource(R.drawable.img1);
                pic3.setImageResource(R.drawable.img1);
                pic4.setImageResource(R.drawable.img1);


            }


        }
    });

}



public void pic1Click(View v){
    pic1.setImageResource(R.drawable.img1);
}
public void pic2Click(View v){
    pic2.setImageResource(R.drawable.img1);
}
public void pic3Click(View v){
    pic3.setImageResource(R.drawable.img2);
}
public void pic4Click(View v){
    pic4.setImageResource(R.drawable.img2);
}

推荐答案

所以我认为您正在寻找一种方法来确定用户单击了哪个 ImageButton .您可以从传递给 onClick 方法的view变量中获取此信息.

So I think you are looking for a way to figure out which ImageButton the user clicked. You can get this from the view variable being passed to the onClick method.

您还需要跟踪单击了哪些图像.您可以通过在图像上添加标签来跟踪此情况.

You also need to keep track of which images have been clicked. You can keep track of this by adding a tag to the image.

public void myClickMethod(View v){
    if (v.getId() == pic1.getId() ) {
        pic1.setImageResource(R.drawable.img2);
        pic1.addTag("img2");
    } else if (v.getId() == pic2.getId() {
        pic2.setImageResource(R.drawable.img2);
        pic2.addTag("img2");
    } else if (v.getId() == pic3.getId() {
        pic3.setImageResource(R.drawable.img3);
        pic3.addTag("img3");
    } else if (v.getId() == pic4.getId() {
        pic4.setImageResource(R.drawable.img3);
        pic4.addTag("img3");
    } 

当然,您将需要在每次更改图像时(以及第一次创建图像时)设置标签.因此,要检查是否需要重置,您需要检查两个图像是否已从它们开始时使用的默认图像更改而来.您也可以使用全局变量来执行此操作,但是标记可能会更直观.

Of course you will want to set the tag every time you change the image (and the first time you create the image). So to check if you need a reset, you would need to check that two images have changed away from the default image they started with. You could also do this with global variables, but tags might be a little more intuitive.

这篇关于我该如何写“如果按钮被点击"?在android studio中的If语句中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆