测验应用的按钮验证

时间:2017-10-04 16:07:28

标签: java android

我正在开发一个测验应用程序,我从模型类中检索数据。一切都工作正常。但是我想为我的答案实现按钮验证。例如,当用户回答问题时,我希望正确答案(按钮)闪烁绿色,而错误的答案闪烁红色,以防用户弄错。

/ This file contains questions from QuestionBank
public class QuestionLibrary{
    // array of questions
    private String mQuestions [] = {
// my questions
 };
    // array of multiple choices for each question
    private String mChoices [][] = {
// array of choices appear here
               };
    // array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
            // correct answers appear here
};



    // method returns number of questions
    public int getLength(){
        return mQuestions.length;
    }

    // method returns question from array textQuestions[] based on array index
    public String getQuestion(int a) {
        String question = mQuestions[a];
        return question;
    }

    // method return a single multiple choice item for question based on array index,
    // based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
    public String getChoice(int index, int num) {
        String choice0 = mChoices[index][num-1];
        return choice0;
    }

    //  method returns correct answer for the question based on array index
    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }
}

public class QuizActivity extends AppCompatActivity {
    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

    Button button1;
    Button button2;
    Button button3;
    Button button4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begineer);

        button1 = (Button) findViewById(R.id.firstOption);
        button2 = (Button) findViewById(R.id.secondOption);
        button3 = (Button) findViewById(R.id.thirdOption);
        button4 = (Button) findViewById(R.id.fourthOption);
        updateQuestion(); //update question
        updateQuizNumber(mquizNumber);

    }

    private void updateQuizNumber(int mquizNumber) {
        msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
    }


    private void updateQuestion() {
        // check if we are not outside array bounds for questions
        if(mQuestionNumber<mQuestionLibrary.getLength() ){
            // set the text for new question, and new 4 alternative to answer on four buttons
           mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
           button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
            button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
            button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
           button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        else {
           Intent intent = new Intent(this, MenuOptions.class);
//            intent.putExtra("score", mScore); // pass the current score to the second screen
            startActivity(intent);
        }
    }

    public void onClick(View view) {
        //all logic for all answers buttons in one method
        Button answer = (Button) view;
        // if the answer is correct, increase the score
        if (answer.getText() == mAnswer){
            Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
// i need to validate correct answer here by making the button blink green
        }else   {
            Toast.makeText(BegineerActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
// i need to validate wrong answer here by making the button blink red

        }
        if (mQuestionNumber < mQuestionLibrary.getLength()) {
            // once user answer the question, we move on to the next one, if any
            updateQuestion();
            updateQuizNumber(mquizNumber);
        } else {

//           
        }


    }



    }



}

3 个答案:

答案 0 :(得分:0)

  

我希望正确答案(按钮)闪烁绿色和错误   一个在用户出错的情况下闪烁红色

您可以像这样更改按钮颜色

 if (answer.getText() == mAnswer){
    Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
    answer.setBackgroundColor(Color.GREEN); // change button color to green
 }else{
    // set button background color to red 
 }

要知道如何闪烁按钮,您可以按照这个答案

How to make a Button blink in Android?

答案 1 :(得分:0)

您可以像下面那样使用它

    if (answer.getText() == mAnswer){
        Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
        answer.setBackgroundColor(Color.GREEN);// This will change button color to green
animation(); // this will blink button
     }else{
        // set button background color to red 
     }
private void animation(){
Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(200); //This will blink button for 2 seconds
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    answer.startAnimation(anim);
}

如果您需要任何澄清,请与我们联系。感谢

答案 2 :(得分:0)

有一个处理程序可以帮助您。你可以这样使用它:

findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.green_layout));
            Handler handler2 = new Handler();
            handler2.postDelayed(new Runnable() {
                public void run() {
                    findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.white_layout));
                    Handler handler3 = new Handler();
                    handler3.postDelayed(new Runnable() {
                        public void run() {
                            findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.green_layout));
                        }
                    }, 200);
                }
            }, 200);

此代码将帮助您每 200 毫秒闪烁一次 textview_layout 或按钮。你可以以毫秒为单位来安排时间,如果你想制作更多的眨眼动画,你需要编写更多嵌套的Handler-s

希望对你有帮助