2016-06-12 39 views
0

所以我想做一个小测验中,我将有4个按钮,用于可能的答案。我想检查是否按下了右键,如果是这样,我想改变图像大约2秒钟,并加载下一个问题(这将是一个图像存储在一个数组),这将是一个for循环的长度问题阵列我相信?的Android Studio的基础测验问题

我有一些麻烦,这一点,因为我还就如何加载一个新的问题不确定,然后将其知道需要按下,例如哪个按钮。问题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;我需要知道如果按下正确的按钮(正确的按钮更改为每个问题)如何循环访问图像数组,并更改按钮上的文本,直到回答所有问题。

任何帮助将提前赞赏,感谢。 :)

+0

是你的图像中的问题?并为您的问题,你会有正确的答案选项? –

+0

我的问题是图像是的,好吧,而我有一个图像为每个问题和下面的标题。正确的答案将出现在按钮上,例如它可能像“这是什么颜色”,然后按下“蓝色”按钮,然后因为你知道它正确,它应该加载下一个图像和答案集。 – flanelman

回答

0

我认为你应该做的最好的事情就是创建一个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 s的数组。

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),而第二个问题是“假“(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. 
} 
+0

嘿谢谢这个回应它实际上很容易遵循(我不是所有的经验)我会给它一会儿去,让你知道我怎么走! :) – flanelman

+0

我做的按钮阵列: 私人按钮btns [] =新的按钮[4]; btns [0] =(Button)findViewById(R.id.Ans1Btn); btns [1] =(Button)findViewById(R.id.Ans2Btn); btns [2] =(Button)findViewById(R.id.Ans3Btn); btns [3] =(Button)findViewById(R.id.Ans4Btn); 但我不确定你的意思是在数组中查看?和按钮的索引,我会怎么做?再次感谢:) – flanelman

+0

@flanelman在您的点击监听器中,只需执行'btns.indexOf(view)'获取索引并将其与当前问题的正确答案索引进行比较即可。 – Sweeper

0

我想给的一般方法,以你的问题,你可以或者可能需要根据自己的需要进行修改,

你有问题的Image Array,想知道correct Button按下或不会,你需要的选项和correct answerButton 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 
+0

它如何知道哪个按钮被点击为“btnClicked”?此外,这将如何循环通过所有的答案?我真的不明白什么是选项1,对不起。 感谢您的回应:) – flanelman

+0

你会为所有按钮设置'onClickListener'吗?那么在所有'onClick()'方法中,你可以编写上面的代码,并且关于上面的option1,我认为你会为所有问题提供一些选项? –

+0

是的,我喜欢。对不起,我不确定哪些选项是?我打算在每个问题的按钮上写下一组答案,每次显示不同的图像。 感谢您的回应btw :) – flanelman