簡體   English   中英

如何防止我的android中的“提交”按鈕在多次單擊時給我多個分數?

[英]How do I prevent my submit button in android from giving me multiple scores when clicked multiple times?

每當我回答問題並單擊“提交”按鈕時,應該顯示1分,但再次單擊“提交”按鈕時,分數將繼續加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);禁用按鈕。 一旦用戶點擊等待時間

  • 在您的情況下,如果您沒有按鈕對象,則可以使用一個全局布爾值並將其設置為false默認值,並在用戶首次單擊時執行該過程,並將該布爾值設置為true,然后從下一次檢查布爾值是否為true只需退貨即可。

私有布爾值mIsSubmited = false;

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

單擊提交按鈕后,可以將其禁用。 例如:

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

防止用戶一次又一次單擊提交。

您可以使用以下命令重新啟用它:

submitbutton.setEnabled(true);

您可以全局聲明布爾值,並首先將其設置為false。 然后單擊按鈕,可以將其值設為true。 在按鈕上單擊,您可以檢查其是否為假,然后僅增加分數。

您可以為此使用處理程序,然后可能有更好的解決方案。

Handler mHandler=new Handler();

//現在在您的點擊監聽器中

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

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

//如果在用戶單擊之前先等待半秒,然后再計算結果,然后再停止計算即可運行,然后再次進行計算。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM