【问题标题】:How to increment score only once even though button is clicked multiple times?即使多次单击按钮,如何仅增加一次分数?
【发布时间】:2015-11-01 01:59:10
【问题描述】:

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

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 新手。

【问题讨论】:

    标签: android button


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      一个简单的方法是添加一个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);
      
          });
      

      或类似的东西。

      【讨论】:

        【解决方案3】:

        点击后禁用按钮的点击

        button.clickable(false);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-18
          • 1970-01-01
          相关资源
          最近更新 更多