即使多次点击按钮,如何只增加一次得分?

时间:2015-10-31 13:49:43

标签: android button

//我正在开发一个测验应用程序,其中我有下一个并提交按钮和两个普通按钮作为选项我正在实现真或假测验。如果我点击任何按钮,它将显示为绿色。例如,如果我在问题1`如果我点击两次正确答案并点击提交,则分数增加两倍。它应该只增加一次得分。任何人都可以帮助我我是android的新手。感谢Advance.Below是我的代码。

public class TrueFalseActivity extends Activity implements View.OnClickListener {
    List<QuestionTrueFalse> questionTrueFalseList;
    int myscore = 0;
    int quid = 0;
    int id;
    TextView tv, txt1;
    QuestionTrueFalse curQues;
    Button b1, b2;
    Button next;
    Button button,submit;
    QuestionTrueFalse cur;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trfal);
        tv = (TextView) findViewById(R.id.tv1);
        txt1 = (TextView) findViewById(R.id.txt);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        submit=(Button) findViewById(R.id.finish);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        if (!timerHasStarted) {

            countDownTimer.start();

            timerHasStarted = true;
        }
        DbHelper db = new DbHelper(this);

        questionTrueFalseList = db.getAllTrFalsQuestions();
        if (questionTrueFalseList != null && questionTrueFalseList.size() != 0) {
            curQues = questionTrueFalseList.get(quid);
            try {
                setQuestionView();
            } catch (NullPointerException e) {
                Toast.makeText(getApplicationContext(), "Exception caught", Toast.LENGTH_LONG).show();
            }
        }


        next = (Button) findViewById(R.id.forw);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button = (Button) v;

                String buttonText = button.getText().toString();

                if (curQues.getAnsw().equals(buttonText)) {

                    myscore++;


                }

                cur = curQues;

                if (quid < 19) {
                    quid++;
                    b1.setBackgroundResource(android.R.drawable.btn_default);
                    b2.setBackgroundResource(android.R.drawable.btn_default);
                    curQues = questionTrueFalseList.get(quid);


                    setQuestionView();

                } else {
                    Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("score", myscore); //Your score
                    intent.putExtras(b); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button = (Button) v;
                String buttonText = button.getText().toString();
                if (curQues.getAnsw().equals(buttonText)) {

                    myscore++;


                }
                Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", myscore); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void setQuestionView() {
        tv.setText(curQues.getQues());
        b1.setText(curQues.getOpt1());
        b2.setText(curQues.getOpt2());

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
               button = (Button) v;
        String buttonText = button.getText().toString();

        if (curQues.getAnsw().equals(buttonText)) {

            myscore++;

        }


        if (quid > 19) {

            Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", myscore); //Your score
            intent.putExtras(b); //Put your score to your next Intent
            startActivity(intent);
            finish();
        }
        switch (v.getId()) {
            case R.id.b1:
                if (v.getId() == R.id.b1) {
                    button.setBackgroundColor(Color.GREEN);
                    b2.setBackgroundResource(android.R.drawable.btn_default);
                }
                break;

            case R.id.b2:
                if (v.getId() == R.id.b2) {
                    button.setBackgroundColor(Color.GREEN);
                    b1.setBackgroundResource(android.R.drawable.btn_default);
                }
                break;
        }


    }


    }
}

如果我点击正确答案两次并按提交分数增加两倍请帮帮我..对Android新手。

3 个答案:

答案 0 :(得分:1)

你可以在Questionanswer类中添加一个额外的布尔变量。 名称已回答

  if (curQues.getAnsw().equals(buttonText)) {
if(IsAnswered == False )
{
IsAnswered == true;
                myscore++;
}
}

答案 1 :(得分:0)

单击

后,使用

禁用单击按钮
button.clickable(false);

答案 2 :(得分:0)

执行所需操作的简便方法是添加Boolean变量,如isSubmited,然后使用if语句,您可以禁用该按钮,直到下一个问题出现。

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button = (Button) v;
            String buttonText = button.getText().toString();
            if (curQues.getAnsw().equals(buttonText)) {
                myscore++;

            if (isSubmited == true)
               submit.setEnable(false);

    });

或类似的东西。