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

标签 java android

每当我回答问题并点击提交按钮时,应该显示 1 分,但再次点击提交按钮时,分数会不断增加 1,并且点击提交按钮的次数会不断增加分数。我实际上不希望每次单击提交按钮时将 1 添加到分数中。我不希望根据我点击提交按钮的次数来更新分数。

包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;
    }
}

最佳答案

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

  • 可以通过button.setEnable(false)禁用该按钮;一旦用户点击 for time

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

私有(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..
        }

关于java - 如何防止 Android 中的提交按钮在多次点击时给出多个分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450428/

相关文章:

android - com.google.android.gms.internal.zzhu : can't find referenced class android. security.NetworkSecurityPolicy

android - 重新采样音频会改变速度

android - SchemaFactory 不支持平台级别 8 中的 W3C XML Schema?

android - ics-openvpn : how to compile on windows machine

android - 如何使用 CursorAdapter 从 ListView 中删除选定的项目

java - fragment 错误,transaction.replace()

java - LibGDX - 在对象的前面或后面显示玩家

java - 如何让客户指定他们的电子邮件服务器信息?

java - 扫描仪类 ~ hasNextDouble();

java - java中如何删除包含某个字符的单词?