0

我正在尝试检查相应的文本是否与按钮中显示的图像相同。

例如显示字符串“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 还是有点陌生​​。我不知道我想要的是否可能。有人可以帮忙。:(

4

1 回答 1

1

您无法将 Drawable 与资源 ID 进行比较,这就是您正在做的事情:

if(img1.getBackground().equals(R.drawable.heart)){
    ...

也许您想要的是在设置时保存背景的资源ID,例如:

this.mImg1Bg = R.drawable.heart;
img1.setBackground(this.mImg1Bg);

然后在您测试的 onClick() 上:

if(this.mImg1Bg == R.drawable.heart) {
    ...

编辑:

现在有了整个代码,就更容易了。

我假设你在 onCreate() 的末尾有这个:

this.myImg1=R.drawable.heart;

仅出于测试目的,因为您正在重新定义它,因为它已经设置好了。

现在字符串测试还有另一个问题。形状变量不是您要测试的。也许如果你像这样测试它:

public void onClick(View v){

    String txt = ((Button)v).getText();
    if(txt.equals("Heart")){
        if(myImg1 == R.drawable.heart){
            correct++;
        } 
    }
    ...
}

我不知道您在活动中到底显示了什么,但在我看来,如果您使用以下内容,您可以简化很多:

HashMap<String, Integer> shapes;
shapes.put("Heart", R.drawable.heart);
...

存储形状字符串和与资源的关系。

一个 ArrayList of Pair,用于存储按钮上的组合:

ArrayList<Pair<Integer,String>> buts;
buts.add(new Pair<Integer,String>(R.drawable.heart, "Heart"));
...

在每个按钮视图上设置一个标签,以了解单击了哪个按钮编号:

img1.setTag(1);
...

然后将相同的 OnClickListener 分配给所有按钮。例如,通过您的 Activity 实现接口,例如:

public class Game extends Activity implements OnClickListener {

...

    img1.setOnClickListener(this);
    ...

并像这样检查 onClick() :

    public void onClick(View v) {
        int res = buts.get((Integer)v.getTag()).first;
        String txt = buts.get((Integer)v.getTag()).second;
        if(res == shapes.get(txt)) {
            correct++;
        } 
    }

然后不要忘记将正确的分配给某些 UI TextView,以便您可以查看结果。

于 2013-08-27T16:27:11.447 回答