在测验应用

时间:2018-01-22 05:34:22

标签: java android

大家好。我有一个有4种选择的多项选择测验应用程序。只需点击任何按钮,问题和分数就会自动更新。没有“下一步”按钮。 我尝试添加此代码 “answer.setBackgroundColor(Color.GREEN);” 以突出显示正确的答案,但问题是,突出显示的按钮不会消失显示下一个问题。请帮忙。

public class SetA extends AppCompatActivity {
....
private TextView mScore;
private TextView mQuestion;
private Button mBtn1;
private Button mBtn2;
private Button mBtn3;
private Button mBtn4;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;


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

    mScore = (TextView)findViewById(R.id.score);
    mQuestion = (TextView)findViewById(R.id.question);
    mBtn1 = (Button)findViewById(R.id.choice1);
    mBtn2 = (Button)findViewById(R.id.choice2);
    mBtn3 = (Button)findViewById(R.id.choice3);
    mBtn4 = (Button)findViewById(R.id.choice4);
    mQuestions.initQuestions(getApplicationContext());
    updateQuestion();
    updateScore(mScore);
}
private void updateQuestion(){
    if(mQuestionNumber<mQuestions.getLength() ){

 mQuestion.setText(mQuestions.getQuestion(mQuestionNumber));

 mBtn1.setText(mQuestions.getChoice(mQuestionNumber, 1));

 mBtn2.setText(mQuestions.getChoice(mQuestionNumber, 2));

 mBtn3.setText(mQuestions.getChoice(mQuestionNumber, 3));


 mBtn4.setText(mQuestions.getChoice(mQuestionNumber,4));
        mAnswer = mQuestions.getCorrectAnswer(mQuestionNumber);
        mQuestionNumber++;
    }
    else {

        Intent intent = new Intent(SetA.this, Scores.class);
        intent.putExtra("score", mScore);
        startActivity(intent);
    }
}
private void updateScore(int point) {
    mScore.setText(""+mScore+"/"+mQuestions.getLength());
}
public void onClick(View view) {
    Button answer = (Button) view;
    if (answer.getText().equals(mAnswer)){
answer.setBackgroundColor(Color.GREEN);
        mScore = mScore + 1;
        Toast.makeText(SetA.this, "Correct!", 
Toast.LENGTH_SHORT).show();
    }else
        Toast.makeText(GESetA.this, "Wrong!", 
Toast.LENGTH_SHORT).show();
    updateScore(mScore);
    updateQuestion();
}
....

2 个答案:

答案 0 :(得分:0)

更新下一个问题时,您可以重置背景颜色:

answer.setBackgroundColor(Color.TRANSPARENT);

答案 1 :(得分:0)

我无法为自己测试这个,但我没有看到你改变颜色的任何地方。如果您在正确答案上突出显示绿色按钮,那么您需要在选择下一个问题时重置颜色的代码。

你需要实现一些知道你已经选择它的东西然后转移到下一个问题。对不起,如果这看起来很模糊,但我想尽力解释它。

基本上,现在突出问题的部分无法知道问题已转移到一个新问题,你需要实现类似的东西。可能像这样的伪代码:

if(question is next question){restore default colours;}

这样的事情。显然,这只是一个真正的基本伪代码。如果您需要更多实质性帮助,我会在今天稍后再看看它,然后亲自测试一下应用程序,看看我能想出什么。

相关问题