我正在尝试为自闭症儿童进行测验。我想使用图片来寻找答案或更好的选择,例如带有https://gyazo.com/2309e055184a4840c23a07bda0a5bf63这样的图片的按钮。我的应用程序可以运行,但是当我进行测验时,我无法获得正确的答案,只有错误。似乎if语句不起作用。我需要四个静态答案,并且只更改ImageView
。我不知道该怎么办。
public class Category2 extends AppCompatActivity{
private database2 database2 = new database2();
private ImageButton imageChoice1,imageChoice2,imageChoice3,imageChoice4;
ImageView iv_Pic;
private Button imageAnswerSpace;
int quizCount = 0;
int rightAnswerCount = 0;
private int mAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category2);
iv_Pic = (ImageView) findViewById(R.id.iv_Pic);
imageChoice1 = (ImageButton) findViewById(R.id.imageview1);
imageChoice2 = (ImageButton) findViewById(R.id.imageview2);
imageChoice3 = (ImageButton) findViewById(R.id.imageview3);
imageChoice4 = (ImageButton) findViewById(R.id.imageview4);
newQ();
imageChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.imageview1 && (v.getId() == mAnswer)) {
Toast.makeText(Category2.this, "correct", Toast.LENGTH_SHORT).show();
newQ();
} else {
Toast.makeText(Category2.this, "wrong", Toast.LENGTH_SHORT).show();
newQ();
}
}
});
imageChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.imageview2 && (v.getId()== mAnswer)){
Toast.makeText(Category2.this, "correct", Toast.LENGTH_SHORT).show();
newQ();
} else {
Toast.makeText(Category2.this, "wrong", Toast.LENGTH_SHORT).show();
newQ();
}
}
});
imageChoice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.imageview3 && (v.getId()== mAnswer)) {
Toast.makeText(Category2.this, "correct", Toast.LENGTH_SHORT).show();
newQ();
} else {
Toast.makeText(Category2.this, "wrong", Toast.LENGTH_SHORT).show();
newQ();
}
}
});
imageChoice4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.imageview4 && (v.getId()== mAnswer)) {
Toast.makeText(Category2.this, "correct", Toast.LENGTH_SHORT).show();
newQ();
} else {
Toast.makeText(Category2.this, "wrong", Toast.LENGTH_SHORT).show();
newQ();
}
}
});
}
public void newQ() {
iv_Pic.setImageResource(database2.getImages(quizCount));
ImageView changeImage0 = findViewById(R.id.imageview1);
changeImage0.setImageResource(database2.getChoice1(quizCount));
ImageView changeImage1 = findViewById(R.id.imageview2);
changeImage1.setImageResource(database2.getChoice2(quizCount));
ImageView changeImage3 = findViewById(R.id.imageview3);
changeImage3.setImageResource(database2.getChoice3(quizCount));
ImageView changeImage4 = findViewById(R.id.imageview4);
changeImage4.setImageResource(database2.getChoice4(quizCount));
mAnswer = database2.getCorrectAnswer(quizCount);
if (quizCount < 4) {
quizCount++;
} else if (quizCount == 4) {
quizCount = 0;
}
}
}
public class database2 {
private Integer mImages[] = {
R.drawable.sad,
R.drawable.happy,
R.drawable.fear,
R.drawable.angry,
};
public static int mChoices[] [] = {
{
R.drawable.ic_angry,
R.drawable.ic_sad,
R.drawable.ic_scared,
R.drawable.ic_smiling
}, {
R.drawable.ic_angry,
R.drawable.ic_sad,
R.drawable.ic_scared,
R.drawable.ic_smiling
}, {
R.drawable.ic_angry,
R.drawable.ic_sad,
R.drawable.ic_scared,
R.drawable.ic_smiling
}, {
R.drawable.ic_angry,
R.drawable.ic_sad,
R.drawable.ic_scared,
R.drawable.ic_smiling
}
};
private Integer mCorrectAnswer[] = {
R.drawable.ic_sad,
R.drawable.ic_smiling,
R.drawable.ic_scared,
R.drawable.ic_angry
};
public Integer getImages(int a){
Integer image = mImages[a];
return image;
}
public Integer getChoice1(int a){
int choice0 = mChoices[a][0];
return choice0;
}
public Integer getChoice2(int a){
int choice1 = mChoices[a][1];
return choice1;
}
public Integer getChoice3(int a){
int choice2 = mChoices[a][2];
return choice2;
}
public Integer getChoice4(int a){
int choice3 = mChoices[a][3];
return choice3;
}
public Integer getCorrectAnswer(int a){
Integer answer = mCorrectAnswer[a];
return answer;
}
}
答案 0 :(得分:1)
您总是看到“错误”答案的原因是,您正在将视图ID与onClick侦听器中的可绘制ID进行比较。 v.getId()== mAnswer
。 mAnswer是其中之一
private Integer mCorrectAnswer[] ={
R.drawable.ic_sad,
R.drawable.ic_smiling,
R.drawable.ic_scared,
R.drawable.ic_angry
};
而v.getId()是
之一R.id.imageview1
R.id.imageview2
R.id.imageview3
R.id.imageview4
作为一种短期解决方案,您可以将mCorrectAnswer更改为使用ImageView id而不是可绘制的id。但是,这可能很难维护,因此最好使用不依赖于资源ID的不同逻辑。
答案 1 :(得分:0)
答案数组应类似于:
private Integer mCorrectAnswer[] ={
R.id.ic_sad,
R.id.ic_smiling,
R.id.ic_scared,
R.id.ic_angry
};
,它必须与mCorrectAnswer.get(quizCount)
进行比较。