如何点击后再按一下按钮?

时间:2017-05-19 08:36:09

标签: java android onclicklistener

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dpText = (TextView) findViewById(R.id.dpText);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);

        updateQuestion(random.nextInt(mdata.questions.length));

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(button1.getText().equals(correctAnswer)){
                    updateQuestion(random.nextInt(mdata.questions.length));


                } else{
                    gameOver();

                }
    });

 button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(button2.getText().equals(correctAnswer)){
                    updateQuestion(random.nextInt(mdata.questions.length));

                } else{
                    gameOver();

                }
            }
        });

我正在制作一个测验应用。我创建了一个方法updateQuestion(),它接受一个随机整数来显示数组中的问题。我遇到的问题是,当用户点击按钮时,它第一次正常工作。当弹出第二个问题时,按钮不会听取答案。在重复点击时,按钮有时会工作。请帮忙,抱歉英语不好

更新问题代码 -

public void updateQuestion(int num){
        dpText.setText(mdata.getQues(num));
        button1.setText(mdata.getAlia(num));
        button2.setText(mdata.getPari(num));
        correctAnswer = mdata.getAnswer(num);

    }

游戏代码 -

private void gameOver(){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder
                .setMessage("Game Over")
                .setCancelable(false)
                .setPositiveButton("New Game",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                finish();

                            }
                        })
                .setNegativeButton("Exit",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();

                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

0 个答案:

没有答案