[英]Android Studio basic quiz issue
因此,我试图进行一个测验,其中我将有4个按钮来提供可能的答案。 我想检查是否按下了右按钮,如果要这样,我想更改图像大约2秒钟并加载下一个问题(它将是存储在数组中的图像),这将在for循环中我相信问题数组?
我对此有些麻烦,例如,我也不确定如何加载新问题,然后知道需要按下哪个按钮。 问题1可能需要按下按钮1,但问题2可能是按钮3,但我不想更改活动。
创建数组并设置图像视图:
private int[] ImageArr = new int[5];
private ImageView image = (ImageView) findViewById(R.id.Question_ImgView);
填充数组:
public void FillImage() {
ImageArr[0] = R.drawable.img1;
ImageArr[1] = R.drawable.img2;
ImageArr[2] = R.drawable.img3;
ImageArr[3] = R.drawable.img4;
ImageArr[4] = R.drawable.img5;}
然后在“ onCreate方法”中调用它以在启动时填充数组
然后我将有一个问题方法,这是我希望前面提到的事情发生的地方。
tldr; 我需要知道如果按下正确的按钮(每个问题正确的按钮更改)时如何遍历图像数组,并更改按钮上的文本,直到所有问题得到回答。
任何帮助将不胜感激,在此先感谢。 :)
我想为您的问题提供一种通用的方法,您可以根据自己的需要进行修改,
您有一个由问题组成的Image Array
,并且想知道correct Button
按下了correct Button
,为此,您将需要选项和correct answer
来与Button clicked
的Button clicked
进行比较,以检查是否按下了正确的Button
,
而且您不需要一次遍历所有问题,您可以做的就是在按下按钮时,将按钮文本与正确答案进行比较,如果正确,则显示下一个问题图像,例如相同的代码示例,
if(btnClicked.getText().toString().equals("Correct Answer")){
questionPos++;
imgView.setImageResource(questionPos);
// Update all the buttons as per the new options
btn1.setText(options1);
// .... like wise update other buttons
}
看看这是否有意义
编辑
按照你问如何知道单击哪个按钮,你可以做这样的事情...
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
if(btn1.getText().toString().equals("Correct Answer")){
questionPos++;
imgView.setImageResource(questionPos);
// Update all the buttons as per the new options
btn1.setText(options1);
}
}
});
// Similarly you can do for all other buttons
我认为您最好的做法是创建一个Question
类。
public class Question {
@DrawableRes
private int imageId;
private String[] answers;
private int correctAnswerIndex;
public Question(int imageId, int correctAnswerIndex, String... answers) {
this.imageId = imageId;
this.correctAnswerIndex = correctAnswerIndex;
this.answers = answers;
}
@DrawableRes
public int getImageId() { return imageId; }
public String[] getAnswers() { return answers; }
public int getCorrectAnswerIndex() { return correctAnswerIndex; }
}
此类代表一个问题。 它有
imageId
字段用于存储问题的图像 answers
字段,用于存储用户可以选择的所有可能答案 correctAnswerIndex
来存储正确的答案。 实际上,这会将正确答案的索引存储在answers
数组中。 假设您将{"blue", "red", "green", "yellow"}
用作answers
数组,并且正确答案为blue
,则将正确答案索引设置为0
。 不懂如何使用? 让我们来看一个例子。
目前,您有一个图像ID数组。 您应该将其更改为Question
数组。
private Question[] questions = new Question[4];
您的fillQuestion
方法将是:
questions[0] = new Question(R.drawable.sweeper_is_handsome, 0, "True", "False", "Maybe", "I don't know");
questions[1] = new Question(R.drawable.no_one_can_beat_jon_skeet_in_reputation, 1, "True", "False", "Maybe", "I don't know");
// you get the idea.
如您所见,第一个问题的答案为“真”( correctAnswerIndex
= 0),第二个问题的答案为“ False”( correctAnswerIndex
= 1)。
现在,您有很多问题。 您如何显示它?
我认为您可以解决如何自己显示图像的问题,所以让我们集中讨论如何使按钮正常工作。
我猜你的按钮会排列成阵列,对吗? 如果不是,那么您就完全错了。 只需将四个按钮添加到数组中,就很简单。
您可以对每个按钮使用相同的单击处理程序。 没关系 在点击监听器中,您有一个像这样的view
参数?
public void buttonOnClick(View view) {
// ⬆︎
// here
}
如果您这样做正确,则view
应该是按钮数组中的一项。 您可以使用indexOf
方法获取按钮在数组中的索引。 如果该指数相匹配correctAnswerIndex
的问题,用户选择了正确答案!
但是您如何知道正在显示哪个问题? 您可以在活动类中创建一个名为questionIndex
的变量来跟踪它! 它应该从0开始,每当您显示一个新问题时,就增加它!
编辑:
这是您单击处理程序上的按钮的样子:
int pressedButtonIndex = btns.indexOf(view);
int correctAnswerIndex = questions[questionIndex].getCorrectAnswerIndex();
if (pressedButtonIndex == correctAnswerIndex) {
// This means the user chose the correct answer
if (questionIndex == 4) {
// This is actually the last question! No need to display the next!
return;
}
// load next question
questionIndex++;
someImageView.setImage(questions[questionIndex].getImageId());
for (int i = 0 ; i < 4 ; i++) {
btns[i].setText(questions[questionIndex].getAnswers()[i]);
}
// YAY! The next question is displayed.
} else {
// User chose the wrong answer, do whatever you want here.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.