我该如何比较图像

时间:2017-10-17 10:08:03

标签: java android imageview

我正在开发一个多选测验应用程序,其中我使用图像作为问题和答案这里的问题是我不知道如何比较数组中的图像。在onclick方法中,我将点击的选项与存储在数组中的正确答案进行比较但是它不起作用,即使我使用setTaggetTag方法进行选择,我也不知道我尝试过的所有代码都有什么问题。

如果你不想回答,请不要 downvote 这个问题。

这是我的代码。

public class counting extends AppCompatActivity {
    ImageView choice_one, choice_two, choice_three, choice_four;
    ImageView question;
    MediaPlayer mp;
    private Questions mQuestions = new Questions();
    private int mAnswer;
    private int mQuestionsLength = mQuestions.mQuestions.length;

    Random r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counting);

        r = new Random();

        choice_one=(ImageView)findViewById(R.id.choice_1);
        choice_two=(ImageView)findViewById(R.id.choice_2);
        choice_three=(ImageView)findViewById(R.id.choice_3);
        choice_four=(ImageView)findViewById(R.id.choice_4);

        choice_one.setTag(1);
        choice_two.setTag(2);
        choice_three.setTag(3);
        choice_four.setTag(4);

        question=(ImageView)findViewById(R.id.question);

        updateQuestions(r.nextInt(mQuestionsLength));

        choice_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_one) == mAnswer ){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_two) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_three) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_four) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });
    }

    private int getImageResource(ImageView iv ){
        return (Integer)iv.getTag();
    }

    private void updateQuestions(int num){
        question.setImageResource(mQuestions.getQuestion(num));
        choice_one.setImageResource(mQuestions.getChoice1(num));
        choice_two.setImageResource(mQuestions.getChoice2(num));
        choice_three.setImageResource(mQuestions.getChoice3(num));
        choice_four.setImageResource(mQuestions.getChoice4(num));

        mAnswer = mQuestions.getAnswer(num);
    } 
}

问题类:

public class Questions {
    public int mQuestions[]={
        R.drawable.onee,  R.drawable.twoe,  R.drawable.threee,  R.drawable.foure, R.drawable.sixe, R.drawable.eighte
    };

    public int mChoices[][]={
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_six,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_eight,  R.drawable.counting_three,  R.drawable.counting_four}

     };

    public int mAnswers[]=
{R.drawable.counting_one,R.drawable.counting_two,R.drawable.counting_three,
        R.drawable.counting_four, R.drawable.counting_six, R.drawable.counting_eight};

    public int getQuestion(int a){
        int question = mQuestions[a];
        return question;
    }

    public int getChoice1(int a){
        int choice = mChoices[a][0];
        return choice;
    }

    public int getChoice2(int a){
        int choice = mChoices[a][1];
        return choice;
    }

    public int getChoice3(int a){
        int choice = mChoices[a][2];
        return choice;
    }

    public int getChoice4(int a){
        int choice = mChoices[a][3];
        return choice;
    }

    public int getAnswer(int a){
        int answer = mAnswers[a];
        return answer;
    }
}

1 个答案:

答案 0 :(得分:1)

你在

中进行了错误的比较
getImageResource(choice_one) == mAnswer

您正在将资源ID与您提供的标记进行比较(1,2,3,4)。这是错误的。

您可以这样做: -

    private void updateQuestions(int num){
        question.setImageResource(mQuestions.getQuestion(num));
        question. setTag(mQuestions.getQuestion(num));
        choice_one.setImageResource(mQuestions.getChoice1(num));
        choice_one.setTag(mQuestions.getChoice1(num));
        choice_two.setImageResource(mQuestions.getChoice2(num));
        choice_two.setTag(mQuestions.getChoice2(num));
        choice_three.setImageResource(mQuestions.getChoice3(num));
        choice_three.setTag(mQuestions.getChoice3(num));
        choice_four.setImageResource(mQuestions.getChoice4(num));
        choice_four.setTag(mQuestions.getChoice4(num));

        mAnswer = mQuestions.getAnswer(num);
    } 

删除您提供的代码(代码下方),然后尝试

        choice_one.setTag(1);
        choice_two.setTag(2);
        choice_three.setTag(3);
        choice_four.setTag(4);