如何防止我在Android中的提交按钮多次点击时给我多个分数?

时间:2017-06-09 06:08:04

标签: java android

每当我回答一个问题并点击提交按钮时,应该显示1分,但是再次点击提交按钮时,它会不断添加1分数,并且提交按钮上的更多匹配会不断为分数添加1。实际上,每次单击提交按钮时,我都不希望将1添加到乐谱中。我不希望根据我点击提交按钮的次数更新分数。

package com.example.android.generalknowledge;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    int baseScore = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void submitResult(View view) {
        RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
        boolean answerIsLargestRailwayStation = largestRailwayStation.isChecked();

        RadioButton insects = (RadioButton) findViewById(R.id.insects);
        boolean answerIsInsects = insects.isChecked();

        CheckBox physicsChemistry = (CheckBox) findViewById(R.id.physics_chemistry);
        boolean physicsChemistryIsAnswer = physicsChemistry.isChecked();


        CheckBox physiologyMedicine = (CheckBox) findViewById(R.id.physiology_medicine);
        boolean physiologyMedicineIsAnswer = physiologyMedicine.isChecked();


        CheckBox literature = (CheckBox) findViewById(R.id.literature);
        boolean literatureIsAnswer = literature.isChecked();


        CheckBox peaceEconomics = (CheckBox) findViewById(R.id.peace_economics);
        boolean peaceEconomicsIsAnswer = peaceEconomics.isChecked();


        RadioButton naziParty = (RadioButton) findViewById(R.id.nazi_party);
        boolean answerIsNaziParty = naziParty.isChecked();


        RadioButton allOfTheAbove = (RadioButton) findViewById(R.id.all_of_the_above);
        boolean answerIsAll = allOfTheAbove.isChecked();


        RadioButton filmFinance = (RadioButton) findViewById(R.id.film_finance);
        boolean answerIsFilmFinance = filmFinance.isChecked();

        EditText enterNameHere = (EditText) findViewById(R.id.name_view);
        String name = enterNameHere.getText().toString();


        EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
        String answer = enterAnswerHere.getText().toString();
        if (enterAnswerHere.getText().toString().equals("Africa")) {
            baseScore += 1 ;
        }


        int finalScore = calculateTotalScore(answerIsLargestRailwayStation, answerIsInsects, physicsChemistryIsAnswer,
                physiologyMedicineIsAnswer, literatureIsAnswer, peaceEconomicsIsAnswer, answerIsNaziParty, answerIsAll, answerIsFilmFinance);

        Toast.makeText(MainActivity.this,
                name + " " + "\n" + "You have a Total Score of " + " " + finalScore + " " + "/10", Toast.LENGTH_LONG).show();
    }

    /**
     * This method is called when any of the radio buttons in question one is selected
     */

    public void onRadioButtonClickedOne(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.largest_railway_station:
                if (checked)
                    // This is the correct answer
                    break;
            case R.id.highest_railway_station:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.longest_railway_station:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.none_of_the_above:
                if (checked)
                    //Wrong answer
                    break;
        }

    }

    /**
     * This method is called when any of the radio buttons in question two is selected
     */

    public void onRadioButtonClickedTwo(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.behaviour_of_human_beings:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.insects:
                if (checked)
                    // This is the correct answer
                    break;
            case R.id.origin_history:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.rock_formation:
                if (checked)
                    // Wrong answer
                    break;
        }

    }

    /**
     * This method is called when the checkboxes for question three are clicked
     */

    public void onCheckboxThreeClicked(View view) {
        //Is the button now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox is clicked
        switch (view.getId()) {
            case R.id.physics_chemistry:
                if (checked)
                    // One of the Correct answers
                    break;
        }
        switch (view.getId()) {
            case R.id.physiology_medicine:
                if (checked)
                    // One of the Correct answers
                    break;
        }
        switch (view.getId()) {
            case R.id.literature:
                if (checked)
                    // One of the Correct answers
                    break;
        }
        switch (view.getId()) {
            case R.id.peace_economics:
                if (checked)
                    // One of the Correct answers
                    break;
        }
    }

    /**
     * This method is called when any of the radio buttons in question four is selected
     */

    public void onRadioButtonClickedFour(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.labour_party:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.nazi_party:
                if (checked)
                    // This is the correct answer
                    break;
            case R.id.leading_party:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.democratic_party:
                if (checked)
                    // Wrong answer
                    break;
        }

    }

    /**
     * This method is called when any of the radio buttons in question six is selected
     */

    public void onRadioButtonClickedSix(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.develop_telescope:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.discovered_jupiter:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.movement_of_pendulum:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.all_of_the_above:
                if (checked)
                    // This is the correct answer
                    break;
        }

    }

    /**
     * This method is called when any of the radio buttons in question seven is selected
     */

    public void onRadioButtonClickedSeven(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.foreign_finance:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.film_finance:
                if (checked)
                    // This is the correct answer
                    break;
            case R.id.federation_football:
                if (checked)
                    // Wrong answer
                    break;
            case R.id.none:
                if (checked)
                    // Wrong answer
                    break;
        }

    }


    private int calculateTotalScore(boolean questionOneAnswer, boolean questionTwoAnswer, boolean questionThreeAnswer1,
                                    boolean questionThreeAnswer2, boolean questionThreeAnswer3, boolean questionThreeAnswer4,
                                    boolean questionFourAnswer, boolean questionSixAnswer, boolean questionSevenAnswer) {

        if (questionOneAnswer) {
            baseScore += 1 ;
        }

        if (questionTwoAnswer) {
            baseScore += 1 ;
        }

        if (questionThreeAnswer1) {
            baseScore += 1 ;
        }

        if (questionThreeAnswer2) {
            baseScore += 1 ;
        }

        if (questionThreeAnswer3) {
            baseScore += 1 ;
        }

        if (questionThreeAnswer4) {
            baseScore += 1 ;
        }

        if (questionFourAnswer) {
            baseScore += 1 ;
        }

        if (questionSixAnswer) {
            baseScore += 1 ;
        }

        if (questionSevenAnswer) {
            baseScore += 1 ;
        }

       // EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
       // String answer = enterAnswerHere.getText().toString();
       // {
        //    if (answer == "Africa") {
        //        baseScore = baseScore + 1;
        //    }
       // }
        return baseScore;
    }
}

4 个答案:

答案 0 :(得分:2)

我们可以通过2种方式实现,

  • 你可以通过button.setEnable(false)来调整按钮;一旦用户点击了一段时间

  • 在您的情况下,如果您没有按钮对象,那么您可以使用一个全局布尔值并将其设置为false默认值,并且一旦用户单击第一次执行该过程并将该布尔值设置为true,从下次开始检查布尔值是否为真,只需返回它。

private boolean mIsSubmited = false;

        public void submitResult(View view) {
            if(mIsSubmited) {
                return;
            }
            mIsSubmited = true;
            RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
            // Remaining code..
        }

答案 1 :(得分:1)

单击后,您可以禁用提交按钮。例如:

onclick() {
   submitbutton.setEnabled(false);
}

阻止用户一次又一次点击提交。

您可以使用以下方式启用它:

submitbutton.setEnabled(true);

答案 2 :(得分:0)

您可以全局声明一个布尔值,并首先将其设置为false。 然后在按钮单击上,您可以将其值转为true。 点击按钮,你可以检查它是否为假,然后只是为了增加分数。

答案 3 :(得分:0)

你可以使用处理程序,这可能是更好的解决方案。

Handler mHandler=new Handler();

//现在在你的点击监听器中

        mHandler.removeCallbacks(mRunnable);//
        mHandler.postDelayed(mRunnable, 500);

        Runnable mRunnable  =new Runnable() {
        @Override
        public void run() {
            //calculate it
        }
    };

//如果用户点击之前计算结果,等待半秒,然后停止从计算中运行,再次进行计算。 希望它有所帮助。