我正在尝试检查相应的文本是否与按钮中显示的图像相同。
例如显示字符串“heart”,我想知道按钮上显示的图像是否是心脏图像,如果加载了正确的图像,当您单击按钮时,计数器会增加。
package com.example.simplegame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Game extends Activity {
public int correct = 0;
ArrayList<Integer> ShapesArray = new ArrayList<Integer>();
ArrayList<String> ShapestoFind = new ArrayList<String>();
TextView Answer;
Button Check;
Button img1;
Button img2;
Button img3;
Button img4;
Button img5;
Button img6;
Button img7;
Button img8;
Button img9;
Random rand = new Random();
public String Shape;
Drawable bgImg1;
private int myImg1;
private int myImg2;
private int myImg3;
private int myImg4;
private int myImg5;
private int myImg6;
private int myImg7;
private int myImg8;
private int myImg9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ShapesArray.add(R.drawable.circle);
ShapesArray.add(R.drawable.heart);
ShapesArray.add(R.drawable.square);
ShapesArray.add(R.drawable.triangle);
ShapestoFind.add("Heart");
ShapestoFind.add("Circle");
ShapestoFind.add("Triangle");
ShapestoFind.add("Square");
int size = ShapesArray.size();
final TextView PName = (TextView) findViewById (R.id.choice);
Answer = (TextView) findViewById (R.id.textAnswer);
img1 = (Button) findViewById (R.id.button1);
img2 = (Button) findViewById (R.id.button2);
img3 = (Button) findViewById (R.id.button3);
img4 = (Button) findViewById (R.id.button4);
img5 = (Button) findViewById (R.id.button5);
img6 = (Button) findViewById (R.id.button6);
img7 = (Button) findViewById (R.id.button7);
img8 = (Button) findViewById (R.id.button8);
img9 = (Button) findViewById (R.id.button9);
Check = (Button) findViewById(R.id.button10);
int rndInt = rand.nextInt(size);
Shape = ShapestoFind.get(rndInt);
PName.setText(Shape);
Collections.shuffle(ShapesArray);
img1.setBackgroundResource(ShapesArray.get(0));
this.myImg1 = ShapesArray.get(0);
img1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(Shape.equals("Heart")){
if(myImg1 == R.drawable.heart){
correct++;
}
}
else if(Shape.equals("Circle")){
if(myImg1 == R.drawable.circle){
correct++;
}
}
else if(Shape.equals("Square")){
if(myImg1 == R.drawable.square){
correct++;
}
}
else if(Shape.equals("Triangle")){
if(myImg1 == R.drawable.triangle){
correct++;
}
}
}
});
Collections.shuffle(ShapesArray);
img2.setBackgroundResource(ShapesArray.get(1));
this.myImg2 = ShapesArray.get(1);
img2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(Shape.equals("Heart")){
if(myImg2 == R.drawable.heart){
correct++;
}
}
if(Shape.equals("Circle")){
if(img2.getBackground().equals(R.drawable.circle)){
correct++;
}
}
if(Shape.equals("Square")){
if(img1.getBackground().equals(R.drawable.square)){
correct++;
}
}
if(Shape.equals("Triangle")){
if(img1.getBackground().equals(R.drawable.triangle)){
correct++;
}
}
}
});
Collections.shuffle(ShapesArray);
img3.setBackgroundResource(ShapesArray.get(0));
Collections.shuffle(ShapesArray);
img4.setBackgroundResource(ShapesArray.get(1));
Collections.shuffle(ShapesArray);
img5.setBackgroundResource(ShapesArray.get(0));
Collections.shuffle(ShapesArray);
img6.setBackgroundResource(ShapesArray.get(1));
Collections.shuffle(ShapesArray);
img7.setBackgroundResource(ShapesArray.get(0));
Collections.shuffle(ShapesArray);
img8.setBackgroundResource(ShapesArray.get(1));
Collections.shuffle(ShapesArray);
img9.setBackgroundResource(ShapesArray.get(0));
this.myImg1=R.drawable.heart;
Check.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Answer.setText(Integer.toString(correct));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
}
我对 Android 还是有点陌生。我不知道我想要的是否可能。有人可以帮忙。:(