android 上的测验:我正在设置一个 onClickListener 来监视按下了哪个答案按钮,并且我正在尝试从表格中访问正确的答案以检查是否给出了分数。问题是问题没有改变(在这种情况下是数字)。
//variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private TextView questionBox;
private Button startButton, answerButton1, answerButton2, answerButton3, answerButton4;
private RadioGroup radioGroup1;
private RadioButton difficulty_easy_radio, difficulty_medium_radio, difficulty_hard_radio;
private String difficulty = "easy";
private int score = 0;
int pointer = 0;
public String correct; // QuestionTables.questionTable[pointer].getCorrectAnswer();
//variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == startButton.getId())
{
//setting the elements of the layout
setContentView(R.layout.activity_playing);
final View controlsView = findViewById(R.id.fullscreen_content_controls2);
final View contentView = findViewById(R.id.fullscreen_content2);
//connecting layout elements with variables
questionBox = (TextView) findViewById(R.id.question_box);
answerButton1 = (Button) findViewById(R.id.answer_button1);
answerButton2 = (Button) findViewById(R.id.answer_button2);
answerButton3 = (Button) findViewById(R.id.answer_button3);
answerButton4 = (Button) findViewById(R.id.answer_button4);
//changing questions *****"pointer" and "correct" are declared globally to avoid:
//Cannot refer to a non-final variable inside an inner class defined in a different method ERROR*****
// don't forget to i++ as i<QuestionTables.questionTable.length=10
//set first question
QuestionTables.shuffleAnswers(pointer);
questionBox.setText(""+pointer);
answerButton1.setText(QuestionTables.questionAnswers[0]);
answerButton2.setText(QuestionTables.questionAnswers[1]);
answerButton3.setText(QuestionTables.questionAnswers[2]);
answerButton4.setText(QuestionTables.questionAnswers[3]);
correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
//button listener
controlsView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.answer_button1:
{
//check if correct
correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
int clickedId = v.getId();
Button clickedButton = (Button) findViewById(clickedId);
if(clickedButton.getText().equals(correct))
score+=100;
pointer++;
//change question
QuestionTables.shuffleAnswers(pointer);
questionBox.setText(""+pointer);
answerButton1.setText(QuestionTables.questionAnswers[0]);
answerButton2.setText(QuestionTables.questionAnswers[1]);
answerButton3.setText(QuestionTables.questionAnswers[2]);
answerButton4.setText(QuestionTables.questionAnswers[3]);
break;
}
case R.id.answer_button2:
{
//check if correct
correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
int clickedId = v.getId();
Button clickedButton = (Button) findViewById(clickedId);
if(clickedButton.getText().equals(correct))
score+=100;
pointer++;
//change question
QuestionTables.shuffleAnswers(pointer);
questionBox.setText(""+pointer);
answerButton1.setText(QuestionTables.questionAnswers[0]);
answerButton2.setText(QuestionTables.questionAnswers[1]);
answerButton3.setText(QuestionTables.questionAnswers[2]);
answerButton4.setText(QuestionTables.questionAnswers[3]);
break;
}
case R.id.answer_button3:
{
//check if correct
correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
int clickedId = v.getId();
Button clickedButton = (Button) findViewById(clickedId);
if(clickedButton.getText().equals(correct))
score+=100;
pointer++;
//change question
QuestionTables.shuffleAnswers(pointer);
questionBox.setText(""+pointer);
answerButton1.setText(QuestionTables.questionAnswers[0]);
answerButton2.setText(QuestionTables.questionAnswers[1]);
answerButton3.setText(QuestionTables.questionAnswers[2]);
answerButton4.setText(QuestionTables.questionAnswers[3]);
break;
}
case R.id.answer_button4:
{
//check if correct
correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
int clickedId = v.getId();
Button clickedButton = (Button) findViewById(clickedId);
if(clickedButton.getText().equals(correct))
score+=100;
pointer++;
//change question
QuestionTables.shuffleAnswers(pointer);
questionBox.setText(""+pointer);
answerButton1.setText(QuestionTables.questionAnswers[0]);
answerButton2.setText(QuestionTables.questionAnswers[1]);
answerButton3.setText(QuestionTables.questionAnswers[2]);
answerButton4.setText(QuestionTables.questionAnswers[3]);
break;
}
}
}
});
}
}
});
请注意,startButton 位于 Activity 的第一个布局中,当按下此布局时,接下来会出现。