安卓:作为编程onClickListener不起作用 [英] Android: onClickListener does not work as programmed

查看:121
本文介绍了安卓:作为编程onClickListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测验android上:
我设置一个onClickListener要监视的回答按钮pressed,我试图从表中获取正确的答案,以检查是否得分给予与否。
问题是,问题并没有改变(数量在这种情况下)。

Quiz on android: I am setting an onClickListener to monitor which answer button is pressed and i am trying to access the correct answer from a table to check if score is given or not. Problem is question doesnt change(number in this case).

//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是活动的第一个布局时是pssed这种布局$ P $随之而来的。

Note that startButton is in the first layout of the activity and when is pressed this layout comes next.

推荐答案

您有很多code,但我假定这就是问题所在:

You have a lot of code, but I'm assuming that this is the problem:

controlsView.setOnClickListener(new View.OnClickListener() {

您给 controlsView 私人 OnClickListener 然后期望它来处理从点击所有的按钮事件。这将失败,因为其他的按钮不使用这种 OnClickListener

You give controlsView a private OnClickListener then expect it to handle click events from all the buttons. This will fail, since the other buttons aren't using this OnClickListener.

请共享监听器。

View.OnClickListener listener = new View.OnClickListener() {//..

然后将其传递给按钮的其余部分。

Then pass it off to the rest of the buttons.

answerButton1.setOnClickListener (listener);
answerButton2.setOnClickListener (listener);
//etc

您还可以使活动实施 OnClickListener 接口。

public class MyActivity extends Activity implements OnClickListener{

@Override
public void onClick (View v)
{
 //implementation
}

然后设置 OnClickListener ,传递出活动实例,而不是

answerButton1.setOnClickListener (MyActivity.this);

这篇关于安卓:作为编程onClickListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆