问题
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.
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?
EDIT:
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);
}
}
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);
}
回答1:
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.
回答2:
You can reference the Android Dev Doc
At first, you need to know what is Event Listeners, and implement it. After you did it,and then you can Register the Event Listeners with your implementation.
Example Code
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
回答3:
Try this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//write your codes here
}
});
}
来源:https://stackoverflow.com/questions/32528545/how-can-i-write-if-button-clicked-in-an-if-statement-in-android-studio