簡體   English   中英

測驗應用程序,更改錯誤的按鈕顏色並針對錯誤的答案選擇糾正答案,Android

[英]Quiz app, change button color of wrong and correct answer on wrong answer selection, android

我正在使用android測驗應用程序,當選擇了錯誤的答案時,我需要更改按鈕的顏色。 如果他們選擇的按鈕錯誤, 或者他們選擇的按鈕正確,我將其設置為已經更改了顏色。 那么,當選擇了不正確的答案時,最好的方法是同時更改具有正確答案的按鈕的顏色的最佳方法是什么? 如果這樣的話。 基本上,當您選擇錯誤的答案時,我需要有2個按鈕來更改顏色。 紅色代表他們選擇的錯誤答案,綠色代表他們顯然沒有選擇的正確答案。 答案也被打亂了,因此不確定如何判斷哪個是正確的。

還嘗試說明問題,並給我一些例子,因為我剛開始濕透所有這些! 它運行在一個片段中,如果有人對如何精簡任何內容或修復錯誤的代碼有任何指示,請隨時告訴我,因為我才剛剛開始學習並需要我所能獲得的所有幫助。

這是我的onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    timesUp = (TextView)getActivity().findViewById(R.id.times_up);
    timesUp.setVisibility(View.INVISIBLE);

    Bundle c;
    c = getArguments();
    String setCategory = c.getString("category");

    QuizHelper db = new QuizHelper(this.getActivity()); // my question bank class
    quesList = db.getAllQuestions(setCategory);  // this will fetch all quetonall questions

    currentQ = quesList.get(qid);

    txtQuestion = (TextView)getActivity().findViewById(R.id.txtQuestion);
    // the textview in which the question will be displayed

    // the buttons,
    // the idea is to set the text of the buttons with the options from question bank
    button1 = (Button)getActivity().findViewById(R.id.button1);
    button2 = (Button)getActivity().findViewById(R.id.button2);
    button3 = (Button)getActivity().findViewById(R.id.button3);
    button4 = (Button)getActivity().findViewById(R.id.button4);

    //set buttons white
    button1.setBackgroundColor(Color.WHITE);
    button2.setBackgroundColor(Color.WHITE);
    button3.setBackgroundColor(Color.WHITE);
    button4.setBackgroundColor(Color.WHITE);

    // the textview in which score will be displayed
    scored = (TextView)getActivity().findViewById(R.id.score);
    scored.setText(getResources().getString(R.string.current_score, score));

    // the timer
    times = (TextView)getActivity().findViewById(R.id.timers);

    // method which will set the things up for our game
    allAnswers.clear();
    setQuestionView();
    times.setText(getResources().getString(R.string.start_time));

    // Start timer
    timer.start();

    // button click listeners
    // passing the button text to other method
    // to check whether the answer is correct or not
    // same for all three buttons
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button1);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button2);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button3);
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button4);
        }
    });

}

這是我的getAnswer()略有縮短)

public void getAnswer(TextView AnswerString) {

    String option = AnswerString.getText().toString();

    //If timer is running stop and restart
    if(timer != null) {
        timer.cancel(); // Stop Timer
        timer.start(); // Start timer
    }

    if (currentQ.getANSWER().equals(option)) {

        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
        score++;
        scored.setText(getResources().getString(R.string.current_score, score));
        AnswerString.setBackgroundColor(Color.GREEN);
    } else {

       //If answer is wrong change button colors

        AnswerString.setBackgroundColor(Color.RED);
        //WrongAnswer.setBackgroundColor(Color.GREEN);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //Load ResultFragment()...                    

            }
        }, 1000);
        return;

    }
    if (qid < 25) {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                // if questions are not over then do this
                currentQ = quesList.get(qid);
                allAnswers.clear();

                //set all buttons white for next question
                button1.setBackgroundColor(Color.WHITE);
                button2.setBackgroundColor(Color.WHITE);
                button3.setBackgroundColor(Color.WHITE);
                button4.setBackgroundColor(Color.WHITE);

                setQuestionView();

            }
        }, 1000);

    } else {

        // if "game over" (qid>25) do this
        //Load ResultFragment()...

}

最后是我的questionView()

private void setQuestionView() {

    allAnswers.add(currentQ.getOPTA());
    allAnswers.add(currentQ.getOPTB());
    allAnswers.add(currentQ.getOPTC());
    allAnswers.add(currentQ.getOPTD());

    Collections.shuffle(allAnswers);

    txtQuestion.setText(currentQ.getQUESTION());
    button1.setText(allAnswers.get(0));
    button2.setText(allAnswers.get(1));
    button3.setText(allAnswers.get(2));
    button4.setText(allAnswers.get(3));

    qid++;
}

謝謝你的協助!

編輯

您可以使用以下按鈕獲取按鈕文字:

// for currentQ.getANSWER().equals(option)
String option = selectedButton.getText();

編輯之前

getAnswer(Button selectedButton)錯誤的答案字段中調用此getAnswer(Button selectedButton)

從數據庫中獲取答案,並將其與所有按鈕的getText()匹配

// because you found out that selected answer/button was wrong
selectedButton.setBackgroundColor(Color.RED);

// take real answer from DB and store in String, probably you've already done this
String answerText = "take-answer-from-database";

選項1:

//match DB answer to selected answer, turn it green if it is correct
if(button1.getText().equals(answerText)){
    button1.setBackgroundColor(Color.GREEN);
} else if(button2.getText().equals(answerText)){
    button2.setBackgroundColor(Color.GREEN);
} else if(button3.getText().equals(answerText)){
    button3.setBackgroundColor(Color.GREEN);
} else if(button4.getText().equals(answerText)){
    button4.setBackgroundColor(Color.GREEN);
}

選項2:

// match DB answer to selected answer, turn it green if it is correct
ArrayList buttonList = new ArrayList();
buttonList.add(button1);
buttonList.add(button2);
buttonList.add(button3);
buttonList.add(button4);

for(Button button : buttonList){
    if(button.getText().equals(answerText)){
        button.setBackgroundColor(Color.GREEN);
        break;
    }
}

注意:如果您的getAnswer(Button)位於不同的類中,則只需將您的按鈕設為global和public ,然后使用其Class對象訪問它們

讓我知道您是否需要更多幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM