-1
我正在做一個簡單的測驗應用程序與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;
}
我搜索互聯網尋求幫助,但無法找到任何東西。誰能幫幫我嗎?謝謝。
使用一個計數器,當任何複選框被選中時遞增,當每個選擇框未被選中時遞減。 –
用戶一個'int'變量計數器。順便說一句,你已經使代碼複雜化,一次又一次地做同樣的事情。你可以創建一個函數並傳遞更新消息的'字符串'('優秀','好'等),而不是一次又一次地重複代碼。而不是使用所有這些布爾值,因爲你的布爾值是按順序排列的,所以你可以使用''布爾值'數組'。 –