0

我有两个活动。一个活动显示一组图像中的一组随机图像。然后在 10 秒后,第二个活动显示一组可点击的随机图像。目标是让用户点击两个活动共有的图像。该应用程序应在文本视图上显示正确点击次数和错误点击次数。我试图比较这两个活动的可绘制对象,但没有运气。请建议我解决这个问题的方法。获取点击图像的资源 ID 会有所帮助。但是我找不到任何可以帮助我获得它的东西。我对 Android 还是很陌生。

MainActivity.java:

public class MainActivity extends Activity
{
int i=5;
static int[] imageViews ={
        R.id.imageView1,R.id.imageView2,R.id.imageView3,
        R.id.imageView4,R.id.imageView5,R.id.imageView6,
        R.id.imageView7,R.id.imageView8,R.id.imageView9,
        R.id.imageView10,R.id.imageView11,R.id.imageView12,
        R.id.imageView13,R.id.imageView14,R.id.imageView15,
        R.id.imageView16,R.id.imageView17,R.id.imageView18
    };

static int[] images = {
    R.drawable.animalsimg1,R.drawable.animalsimg2,R.drawable.animalsimg3,           R.drawable.animalsimg4,R.drawable.animalsimg5,R.drawable.animalsimg6,
        R.drawable.animalsimg7,R.drawable.animalsimg8,R.drawable.animalsimg9,
        R.drawable.animalsimg10,R.drawable.animalsimg11,R.drawable.animalsimg12,
        R.drawable.animalsimg13,R.drawable.animalsimg14,R.drawable.animalsimg15,
        R.drawable.animalsimg16,R.drawable.animalsimg17,R.drawable.animalsimg18,
        R.drawable.animalsimg19,R.drawable.animalsimg20
    };

int[] a;
int[] b;
static int[] c=new int[5];



@Override
protected void onCreate(Bundle savedInstanceState) 
{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    a=RandomizeArray(images);
    b=RandomizeArray(imageViews);


    for(int j=0;j<4;j++){

        int imv = b[j];
        int im =  a[j];             
        ImageView iv = (ImageView)findViewById(imv);
        iv.setImageResource(im);
        c[j]=a[j];

    }







        new CountDownTimer(7000, 1000) {

        TextView timertext=(TextView)findViewById(R.id.textView1);

        public void onTick(long millisUntilFinished) {
         timertext.setText("seconds remaining: " + millisUntilFinished / 1000);

        }

        public void onFinish() {
            timertext.setText("done!");

            finish();
            Intent intent=new Intent(MainActivity.this,SecondActivity.class);
            startActivity(intent);


        }
        }.start();


    }

public static int[] RandomizeArray(int[] array){
    Random rgen = new Random();  // Random number generator         

    for (int i=0; i<array.length; i++) {
        int randomPosition = rgen.nextInt(array.length);
        int temp = array[i];
        array[i] = array[randomPosition];
        array[randomPosition] = temp;
    }

    return array;
}

}

第二活动.java:

public class SecondActivity extends Activity{


int[] a;
int[] b;
int p=0;
int im;
int imv;
int[] c=new int[4];
int j;
int correct=0;
int wrong=0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    tv=(TextView)findViewById(R.id.textView1);


    a=RandomizeArray(MainActivity.images);
    b=RandomizeArray(MainActivity.imageViews);


    for(j=0;j<4;j++){

        imv = b[j];
        im =  a[j];             
        ImageView iv = (ImageView)findViewById(imv);
        iv.setImageResource(im); 

    }
}


    public void onClick(View v){


        for(p=0;p<4;p++){
            int s=c[p];
            if(((ImageView) v).getDrawable().getConstantState().equals(getResources().getDrawable(MainActivity.images[s]).getConstantState())){
                correct++;
            }   
            else{
                wrong++;
            }


        v.setBackgroundResource(R.drawable.vattom_2);
        tv.setText("Correct="+Integer.toString(correct)+" Wrong="+Integer.toString(wrong));
        }
    }


    public static int[] RandomizeArray(int[] array){
        Random rgen = new Random();  // Random number  generator            

        for (int i=0; i<array.length; i++) {
            int randomPosition = rgen.nextInt(array.length);
            int temp = array[i];
            array[i] = array[randomPosition];
            array[randomPosition] = temp;
        }

        return array;
    }
   }
4

1 回答 1

0

您应该考虑使用标签,我相信它可以解决您的问题,因为它们很容易比较

于 2013-10-27T19:01:33.467 回答