如何限制复选框选择

时间:2017-10-07 13:39:47

标签: java android

我正在制作一个包含5个问题的简单测验应用。有2个正确的答案和6个复选框可供选择。如何设置它以便在选中2个复选框时(对于每个问题),应用程序会阻止用户再选择并显示Toast消息,指出您不能为每个问题检查2个以上的复选框?

这是我的代码(Java,在Android Studio中):

package com.example.android.architecturequizapp;

import android.content.Context;
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.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

//method called when Submit&Grade button is clicked
public void submitAnswers(View view) {

    //EditText name
    EditText nameTypeTextBox = (EditText) findViewById(R.id.name_edit_text);
    String typeName = nameTypeTextBox.getText().toString();

    //Question 1: Name Question: Eiffel Tower
    RadioButton q1Name = (RadioButton) findViewById(R.id.q1_eiffel_tower_radio_button);
    boolean hasQ1NameCorrect = q1Name.isChecked();

    //Question 1: Location Question: Paris
    CheckBox q1Location1 = (CheckBox) findViewById(R.id.q1_paris_checkbox);
    boolean hasQ1Location1Correct = q1Location1.isChecked();

    //Question 1: Location Question: France
    CheckBox q1Location2 = (CheckBox) findViewById(R.id.q1_france_checkbox);
    boolean hasQ1Location2Correct = q1Location2.isChecked();

    //Question 2: Name Question: Colosseum
    RadioButton q2Name = (RadioButton) findViewById(R.id.q2_colosseum_radio_button);
    boolean hasQ2NameCorrect = q2Name.isChecked();

    //Question 2: Location Question: Rome
    CheckBox q2Location1 = (CheckBox) findViewById(R.id.q2_rome_checkbox);
    boolean hasQ2Location1Correct = q2Location1.isChecked();

    //Question 2: Location Question: Italy
    CheckBox q2Location2 = (CheckBox) findViewById(R.id.q2_italy_checkbox);
    boolean hasQ2Location2Correct = q2Location2.isChecked();

    //Question 3: Name Question: Tower of Pisa
    RadioButton q3Name = (RadioButton) findViewById(R.id.q3_tower_of_pisa_radio_button);
    boolean hasQ3NameCorrect = q3Name.isChecked();

    //Question 3: Location Question: Pisa
    CheckBox q3Location1 = (CheckBox) findViewById(R.id.q3_pisa_checkbox);
    boolean hasQ3Location1Correct = q3Location1.isChecked();

    //Question 3: Location Question: Italy
    CheckBox q3Location2 = (CheckBox) findViewById(R.id.q3_italy_checkbox);
    boolean hasQ3Location2Correct = q3Location2.isChecked();

    //Question 4: Name Question: Casa Batllo
    RadioButton q4Name = (RadioButton) findViewById(R.id.q4_casa_batllo_radio_button);
    boolean hasQ4NameCorrect = q4Name.isChecked();

    //Question 4: Location Question: Barcelona
    CheckBox q4Location1 = (CheckBox) findViewById(R.id.q4_barcelona_checkbox);
    boolean hasQ4Location1Correct = q4Location1.isChecked();

    //Question 4: Location Question: Spain
    CheckBox q4Location2 = (CheckBox) findViewById(R.id.q4_spain_checkbox);
    boolean hasQ4Location2Correct = q4Location2.isChecked();

    //Question 5: Name Question: Eiffel Tower
    RadioButton q5Name = (RadioButton) findViewById(R.id.q5_opera_house_radio_button);
    boolean hasQ5NameCorrect = q5Name.isChecked();

    //Question 5: Location Question: Sydney
    CheckBox q5Location1 = (CheckBox) findViewById(R.id.q5_sydney_checkbox);
    boolean hasQ5Location1Correct = q5Location1.isChecked();

    //Question 5: Location Question: Australia
    CheckBox q5Location2 = (CheckBox) findViewById(R.id.q5_australia_checkbox);
    boolean hasQ5Location2Correct = q5Location2.isChecked();

    int questionsCorrect = calculateRightAnswers(hasQ1NameCorrect, hasQ1Location1Correct, hasQ1Location2Correct, hasQ2NameCorrect, hasQ2Location1Correct, hasQ2Location2Correct, hasQ3NameCorrect, hasQ3Location1Correct, hasQ3Location2Correct, hasQ4NameCorrect, hasQ4Location1Correct, hasQ4Location2Correct, hasQ5NameCorrect, hasQ5Location1Correct, hasQ5Location2Correct);

    //toast message for questions correct/score
    //less than 5 = poor results
    if (questionsCorrect <= 5) {
        Context context = getApplicationContext();
        CharSequence text = "Poor results! You scored " + questionsCorrect + "/15!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    } else if (questionsCorrect <= 10) {
        Context context = getApplicationContext();
        CharSequence text = "Average results! You scored " + questionsCorrect + "/15!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    } else if (questionsCorrect <= 14) {
        Context context = getApplicationContext();
        CharSequence text = "Good results! You scored " + questionsCorrect + "/15!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    } else {
        Context context = getApplicationContext();
        CharSequence text = "Excellent! You scored " + questionsCorrect + "/15!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

    //TextView message on bottom
    endQuiz(typeName);

}

//calculate total points scored on quiz
private int calculateRightAnswers(boolean q1Name, boolean q1Location1, boolean q1Location2, boolean q2Name, boolean q2Location1, boolean q2Location2, boolean q3Name, boolean q3Location1, boolean q3Location2, boolean q4Name, boolean q4Location1, boolean q4Location2, boolean q5Name, boolean q5Location1, boolean q5Location2) {
    //score before taking quiz
    int pointsScored = 0;

    //1 part of Q1 is correct = + 1 point
    if (q1Name) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q1 is correct = + 1 point
    if (q1Location1) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q1 is correct = + 1 point
    if (q1Location2) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q2 is correct = + 1 point
    if (q2Name) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q2 is correct = + 1 point
    if (q2Location1) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q2 is correct = + 1 point
    if (q2Location2) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q3 is correct = + 1 point
    if (q3Name) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q3 is correct = + 1 point
    if (q3Location1) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q3 is correct = + 1 point
    if (q3Location2) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q4 is correct = + 1 point
    if (q4Name) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q4 is correct = + 1 point
    if (q4Location1) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q4 is correct = + 1 point
    if (q4Location2) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q5 is correct = + 1 point
    if (q5Name) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q5 is correct = + 1 point
    if (q5Location1) {
        pointsScored = pointsScored + 1;
    }

    //1 part of Q5 is correct = + 1 point
    if (q5Location2) {
        pointsScored = pointsScored + 1;
    }

    return pointsScored;
}

private String endQuiz(String nameTyped) {
    TextView endingMessageTextView = (TextView) findViewById(R.id.ending_message_text_view);
    String endQuizMessage = "Thank you " + nameTyped + " for completing the quiz!";
    endQuizMessage += "\nHope you enjoyed it!";
    endingMessageTextView.setText(endQuizMessage);
    return endQuizMessage;
}

我在互联网上寻求帮助,却找不到任何东西。谁能帮帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

您可以按照here使用checkbox.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP){ // check if there are already two checkboxes selected // you can use checkbox.isChecked() and a loop for that Toast.makeText(getApplicationContext(), "Only two checkboxes allowed", Toast.LENGTH_SHORT).show(); return true; //this will prevent checkbox from changing state } return false; // this is default and lets event propagate } });

Notes

另一个选项可能是在第二个复选框设置时禁用所有未选中的复选框,并在取消选中一个时再次启用它们。