2016-06-12 76 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