gpt4 book ai didi

java - 如何在 android studio 的 If 语句中写入 "if button clicked"?

转载 作者:行者123 更新时间:2023-11-30 01:54:11 25 4
gpt4 key购买 nike

我正在用四张牌 (2x2) 构建一个内存游戏。这四张卡片有一个名为“卡片”的 onClick。此 onClick 包含一个 If 语句,如果卡片不相同,则将卡片翻转回去,如果卡片相同,则保留它们。

4 的卡片正面图像相同,但背面图像不同。我的问题是我想让卡片翻转,但它们已经有一个 onClick。那么如何在 If 语句中编写“如果按钮被单击”或者是否有其他解决方案?

编辑:

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);


}


}

我正在尝试的第二种方法:@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 变量中获取它。

您还需要跟踪点击了哪些图像。您可以通过向图像添加标签来跟踪这一点。

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");
}

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

关于java - 如何在 android studio 的 If 语句中写入 "if button clicked"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32528545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com