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位于活动的第一个布局中,按下时会显示此布局。
答案 0 :(得分:2)
你有很多代码,但我认为这是问题所在:
controlsView.setOnClickListener(new View.OnClickListener() {
您为controlsView
提供私有OnClickListener
,然后期望它处理来自所有按钮的点击事件。这将失败,因为其他按钮未使用此OnClickListener
。
建立共享监听器。
View.OnClickListener listener = new View.OnClickListener() {//..
然后将其传递给其余的按钮。
answerButton1.setOnClickListener (listener);
answerButton2.setOnClickListener (listener);
//etc
您还可以让Activity
实现OnClickListener
界面。
public class MyActivity extends Activity implements OnClickListener{
@Override
public void onClick (View v)
{
//implementation
}
然后设置OnClickListener
,您将取消Activity
实例。
answerButton1.setOnClickListener (MyActivity.this);