【问题标题】:Android button onclick doesn't workAndroid按钮onclick不起作用
【发布时间】:2017-03-06 11:05:09
【问题描述】:

我的琐事应用程序出现问题,它可以显示第一个问题,但是当我单击答案(按钮)时,什么也没发生。单击答案按钮时,用户将获得分数并添加总答案。但是,如果用户选择了错误的答案,用户仍会转到下一个问题。

public class play extends Activity implements OnClickListener {
private Question currentQuestion;
private int currentQuestionIndex;
private ArrayList<Button> questionButton;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
public static TextView displayscore;
public static int score;
private List<Question> QuestionList;
private int answerchoice; 
public static int totalanswer;
public static int correctanswer;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);
        Log.i("playclass", "this is play class running");

        questionButton = new ArrayList<Button>();

        questionButton.add((Button) findViewById (R.id.answerbutton1));
        questionButton.add((Button) findViewById (R.id.answerbutton2));
        questionButton.add((Button) findViewById (R.id.answerbutton3));
        questionButton.add((Button) findViewById (R.id.answerbutton4));
        currentQuestion = null;
        currentQuestionIndex = 0;

        View AnswerButton1 = findViewById(R.id.answerbutton1);
        AnswerButton1.setOnClickListener(this);
        View AnswerButton2 = findViewById(R.id.answerbutton2);
        AnswerButton2.setOnClickListener(this);
        View AnswerButton3 = findViewById(R.id.answerbutton3);
        AnswerButton3.setOnClickListener(this);
        View AnswerButton4 = findViewById(R.id.answerbutton4);
        AnswerButton4.setOnClickListener(this);

        Log.i("playclass", "aftersetlistener");

        QuestionList = new ArrayList<Question>();
        ArrayList <String> answer = new ArrayList<String>();

        answer.add("8");
        answer.add("9");
        answer.add("3");
        answer.add("1");
        QuestionList.add(new Question("what is 4+4", answer, 0));
        answer.add("17");
        answer.add("20");
        answer.add("15");
        answer.add("14");
        QuestionList.add(new Question("what is 7+8?", answer, 3));
        answer.add("20");
        answer.add("30");
        answer.add("19");
        answer.add("34");
        QuestionList.add(new Question("what is 10+10?", answer, 0));
        answer.add("12");
        answer.add("11");
        answer.add("13");
        answer.add("14");
        QuestionList.add(new Question("what is 6+6?", answer, 0));
        answer.add("6");
        answer.add("5");
        answer.add("4");
        answer.add("7");
        QuestionList.add(new Question("what is 4+3?", answer, 3));
        answer.add("7");
        answer.add("9");
        answer.add("10");
        answer.add("11");
        QuestionList.add(new Question("what is 3+7?", answer, 2));

        questionstextview = (TextView) findViewById (R.id.questionstextview);           
        questionnumber = (TextView) findViewById (R.id.questionnumber);        
        displayscore = (TextView) findViewById (R.id.displayscore);

        StartTrivia();
    }

public void ButtonPress (View answerButton){
    Log.i("playclass", "after View answerButton ");
    MediaPlayer soundfx = MediaPlayer.create(getApplicationContext(), R.raw.click);

    soundfx.start();

    Log.i("playclass", "after soundfx ");
    for (int i=0; i< questionButton.size(); i++)

        if (questionButton.get(i) ==answerButton)

        if (i==currentQuestion.getAnswerIndex()){
            Log.i("playclass", "before adding score,total answer and correctanswer ");
             score=+5;
             totalanswer++;
             correctanswer++;
             Log.i("playclass", "after adding score,total answer and correctanswer ");

            displayscore.setText(Integer.toString(score));

            Log.i("playclass", "after display score/setscore to integer ");

            Toast.makeText(getApplicationContext(), "Correct!!", Toast.LENGTH_SHORT).show();

             Log.i("playclass", "after correct toast ");
        }
        else{
            Toast.makeText(getApplicationContext(), "Incorrect!!", Toast.LENGTH_SHORT).show();
             Log.i("playclass", "after incorrect toast");
        }

            currentQuestionIndex++;
             Log.i("playclass", "after currentQuestionIndex++; ");

            if (currentQuestionIndex < QuestionList.size()){
                Log.i("playclass", "after currentQuestionIndex < QuestionList.size() ");
                StartTrivia();
                Log.i("playclass", "after StartTrivia in if statement ");
            }
            else{
                Intent result = new Intent (this, finalscreen.class);
                startActivity(result);
                Log.i("playclass", "after IntentResult ");
            }
}

public void StartTrivia(){
    Log.i("playclass", "running StartTrivia() " + currentQuestion); 

    currentQuestion = QuestionList.get(currentQuestionIndex); 
    Log.i("playclass", "after get current question " + currentQuestion);
    questionstextview.setText(currentQuestion.getquestion());
    Log.i("playclass", "after set current question");
    questionnumber.setText(Integer.toString(currentQuestionIndex+1));
    Log.i("playclass", "after convert int to string for question number");
    for (int i = 0; i < questionButton.size(); i++)
     { 
        Log.i("playclass", "before get question button");
        Log.i("playclass", "before currentQuestion " + i + currentQuestion.getanswer());
         String ans = currentQuestion.getanswer().get(i); 
         questionButton.get(i).setText(ans);
         Log.i("playclass", "after get question button");
     }
    } <br><br>

播放 XML 按钮

    <Button
        android:id="@+id/answerbutton1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonPress"
        android:text="Button" />
    <Button
        android:id="@+id/answerbutton2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonPress"
        android:text="Button" />
    <Button
        android:id="@+id/answerbutton3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonPress"
        android:text="Button" />
    <Button
        android:id="@+id/answerbutton4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonPress"
        android:text="Button" />
</LinearLayout>

</LinearLayout><br><br>

LogCat

08-12 14:11:26.853: I/playclass(330): 这是正在运行的游戏类
08-12 14:11:26.853: I/playclass(330): aftersetlistener
08-12 14:11:26.873: I/playclass(330): 运行 StartTrivia() null
08-12 14:11:26.873: I/playclass(330): 得到当前问题后
com.example.quizgame.Question@408feec8
08-12 14:11:26.873: I/playclass(330): 设置当前问题后
08-12 14:11:26.873: I/playclass(330): 将 int 转换为问题号字符串后
08-12 14:11:26.873: I/playclass(330): 在获取问题按钮之前
08-12 14:11:26.873: I/playclass(330): 在当前问题 0[8, 9, 3, 1, 17, 20, 15, 14, 20, 30, 19, 34, 12, 11, 13, 14、6、5、4、7、7、9、10、11]
08-12 14:11:26.883: I/playclass(330): 得到问题按钮后
08-12 14:11:26.883: I/playclass(330): 在获取问题按钮之前
08-12 14:11:26.883: I/playclass(330): 在当前问题 1[8, 9, 3, 1, 17, 20, 15, 14, 20, 30, 19, 34, 12, 11, 13, 14、6、5、4、7、7、9、10、11]
08-12 14:11:26.883: I/playclass(330): 得到问题按钮后
08-12 14:11:26.883: I/playclass(330): 在获取问题按钮之前
08-12 14:11:26.883: I/playclass(330): 在当前问题 2[8, 9, 3, 1, 17, 20, 15, 14, 20, 30, 19, 34, 12, 11, 13, 14、6、5、4、7、7、9、10、11]
08-12 14:11:26.883: I/playclass(330): 得到问题按钮后
08-12 14:11:26.883: I/playclass(330): 在获取问题按钮之前
08-12 14:11:26.893: I/playclass(330): 在当前问题 3[8, 9, 3, 1, 17, 20, 15, 14, 20, 30, 19, 34, 12, 11, 13, 14、6、5、4、7、7、9、10、11]
08-12 14:11:26.893: I/playclass(330): 得到问题按钮后
08-12 14:11:27.423: I/ActivityManager(74): 显示 com.example.quizgame/.play: +722ms

【问题讨论】:

  • 只是为了确定,您发布的 xml 是 play.xml 吗?

标签: android button onclick


【解决方案1】:

如果在 XML 中使用android:onclick,则无需在代码中为按钮设置 OnClickListener。删除代码中的所有setOnClickListener() 内容以及implements OnClickListener,它应该可以工作。

【讨论】:

    【解决方案2】:

    请不要使用

    android:onClick="ButtonPress"
    

    来自 XML 文件。

    如果您想使用多个按钮,请这样使用:

    public class MainActivity extends Activity implements OnClickListener {
    
        private final static String TAG = MainActivity.class.getSimpleName();
    
        Button sendButton, otherButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sendButton = (Button) findViewById(R.id.sendButton);
            sendButton.setOnClickListener(this);
            otherButton = (Button) findViewById(R.id.otherButton);
            otherButton.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
    
            switch (v.getId()) {
    
                case R.id.sendButton:
                    // do something with the button click
                    break;
    
                case R.id.otherButton:
                    // do something
                    break;
            }
        }
    }
    

    这样可以避免奇怪的行为。

    【讨论】:

      【解决方案3】:

      你不需要使用 android:onClick="ButtonPress" 您可以对每个按钮直接使用以下代码

      answerbutton1.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //your code for the particular button 
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2012-03-26
        • 1970-01-01
        • 2018-08-02
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        • 2020-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多