-1

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 的第一个布局中,当按下此布局时,接下来会出现。

4

1 回答 1

2

你有很多代码,但我假设这是问题所在:

controlsView.setOnClickListener(new View.OnClickListener() {

你给controlsView一个私有OnClickListener然后期望它处理来自所有按钮的点击事件。这将失败,因为其他按钮没有使用 this 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);
于 2013-01-28T21:12:22.993 回答