2016-10-29 121 views
0

我正在嘗試做一個小測驗應用程序。不知何故,我有一些麻煩,用測驗的不正確答案來建立敬酒。它沒有正確更新全局變量。此外,我想知道如何更新只有實際錯誤的數字的變量。例如,如果單擊「檢查我的答案」按鈕多次,它會顯示例如「有問題,檢查問題編號:3 4 3 4 3 4」。更新變量

我activity_main看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true"> 

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="1"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="220dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/two" /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.example.android.gtaquizapp.MainActivity"> 

     <TextView 
      style="@style/HeaderTextViewFormat" 
      android:text="@string/QuestionOne" /> 

     <EditText 
      android:id="@+id/question_one_answer_box" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textCapWords" 
      android:isScrollContainer="true" 
      android:maxLength="16" 
      android:maxLines="1" /> 

     <TextView 
      style="@style/HeaderTextViewFormat" 
      android:layout_marginTop="24dp" 
      android:text="@string/QuestionTwo" /> 

     <RadioGroup 
      android:id="@+id/question_two_radio_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_vertical_margin"> 

      <RadioButton 
       android:id="@+id/question_two_radio_one" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerTwo_one" /> 

      <RadioButton 
       android:id="@+id/question_two_radio_two" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerTwo_two" /> 

      <RadioButton 
       android:id="@+id/question_two_radio_three" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerTwo_three" /> 

     </RadioGroup> 

     <TextView 
      style="@style/HeaderTextViewFormat" 
      android:layout_marginTop="24dp" 
      android:text="@string/QuestionThree" /> 

     <CheckBox 
      android:id="@+id/question_three_checkbox_one" 
      style="@style/RadioAndCheckBoxStyle" 
      android:text="@string/AnswerThree_one" /> 

     <CheckBox 
      android:id="@+id/question_three_checkbox_two" 
      style="@style/RadioAndCheckBoxStyle" 
      android:text="@string/AnswerThree_two" /> 

     <CheckBox 
      android:id="@+id/question_three_checkbox_three" 
      style="@style/RadioAndCheckBoxStyle" 
      android:text="@string/AnswerThree_three" /> 

     <CheckBox 
      android:id="@+id/question_three_checkbox_four" 
      style="@style/RadioAndCheckBoxStyle" 
      android:text="@string/AnswerThree_four" /> 

     <TextView 
      style="@style/HeaderTextViewFormat" 
      android:layout_marginTop="24dp" 
      android:text="@string/QuestionFour" /> 

     <RadioGroup 
      android:id="@+id/question_four_radio_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_vertical_margin"> 

      <RadioButton 
       android:id="@+id/question_four_radio_one" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFour_one" /> 

      <RadioButton 
       android:id="@+id/question_four_radio_two" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFour_two" /> 

      <RadioButton 
       android:id="@+id/question_four_radio_three" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFour_three" /> 

      <RadioButton 
       android:id="@+id/question_four_radio_four" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFour_four" /> 
     </RadioGroup> 

     <TextView 
      style="@style/HeaderTextViewFormat" 
      android:layout_marginTop="24dp" 
      android:text="@string/QuestionFive" /> 

     <RadioGroup 
      android:id = "@+id/question_five_radio_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_vertical_margin"> 

      <RadioButton 
       android:id="@+id/question_five_radio_two" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFive_two" /> 

      <RadioButton 
       android:id="@+i`enter code here`d/question_five_radio_one" 
       style="@style/RadioAndCheckBoxStyle" 
       android:text="@string/AnswerFive_one" /> 
     </RadioGroup> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="24dp" 
      android:onClick="checkAnswersMethod" 
      android:text="@string/CheckAnswersButtonText" /> 

    </LinearLayout> 
</TableLayout> 

我的MainActivity看起來是這樣的:

package com.example.android.gtaquizapp; 

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.RadioGroup; 
import android.widget.Toast; 

import static com.example.android.gtaquizapp.R.id.question_four_radio_four; 
import static com.example.android.gtaquizapp.R.id.question_four_radio_three; 
import static com.example.android.gtaquizapp.R.id.question_two_radio_three; 

public class MainActivity extends AppCompatActivity { 

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

/** 
*Global variables are updated by the methods answerOne();, answerTwo(); answerThree(); answerFour(); answerFive(); 
*/ 
private boolean questionOne = false; 
private boolean questionTwo = false; 
private boolean questionThree = false; 
private boolean questionFour = false; 
private boolean questionFive = false; 
private String checkedTextNotAllCorrect = "Something is wrong, check question nr.:"; 

/** 
* This method is executed if the button "Check My Answers" is clicked. 
* It creates a toast with the String checkedTextAllCorrect if all answers are correct or with checkedTextNotAllCorrect if something is wrong 
*/ 
public void checkAnswersMethod(View v) { 
    String checkedTextAllCorrect = "Everything is correct, great job!"; 
    answerOne(); 
    answerTwo(); 
    answerThree(); 
    answerFour(); 
    answerFive(); 

    if (questionOne & questionTwo & questionThree & questionFour & questionFive) { 
     Toast.makeText(getApplicationContext(), checkedTextAllCorrect, Toast.LENGTH_SHORT).show(); 
    } else { 
     Toast.makeText(getApplicationContext(), checkedTextNotAllCorrect, Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Updates the global variable questionOne if the answer was correct 
* Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct 
*/ 
public void answerOne() { 
    EditText questionOneAnswer = (EditText) findViewById(R.id.question_one_answer_box); 
    if (questionOneAnswer.getText().toString().equals("Grand Theft Auto")) { 
     questionOne = true; 
    } else { 
     checkedTextNotAllCorrect += " 1"; 
    } 
} 

/** 
* Updates the global variable questionTwo if the answer was correct 
* Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct 
*/ 
public void answerTwo() { 
    RadioGroup questionTwoRadioGroup = (RadioGroup) findViewById(R.id.question_two_radio_group); 
    int radioButtonID = questionTwoRadioGroup.getCheckedRadioButtonId(); 

    switch (radioButtonID) { 
     case R.id.question_two_radio_one | question_two_radio_three: 
      checkedTextNotAllCorrect += " 2"; 

     case R.id.question_two_radio_two: 
      questionTwo = true; 
    } 
} 

/** 
* Updates the global variable questionThree if the answer was correct 
* Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct 
*/ 
public void answerThree() { 
    CheckBox checkBoxOne = (CheckBox) findViewById(R.id.question_three_checkbox_one); 
    CheckBox checkBoxTwo = (CheckBox) findViewById(R.id.question_three_checkbox_two); 
    CheckBox checkBoxThree = (CheckBox) findViewById(R.id.question_three_checkbox_three); 
    CheckBox checkBoxFour = (CheckBox) findViewById(R.id.question_three_checkbox_four); 

    if (checkBoxTwo.isChecked() & checkBoxThree.isChecked() & checkBoxFour.isChecked() & !checkBoxOne.isChecked()) { 
     questionThree = true; 
    } else { 
     checkedTextNotAllCorrect += " 3"; 
    } 

} 

/** 
* Updates the global variable questionFour if the answer was correct 
* Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct 
*/ 
public void answerFour() { 
    RadioGroup questionFourRadioGroup = (RadioGroup) findViewById(R.id.question_four_radio_group); 
    int radioButtonID = questionFourRadioGroup.getCheckedRadioButtonId(); 

    switch (radioButtonID) { 
     case R.id.question_four_radio_one: 
      questionFour = true; 

     case R.id.question_four_radio_two | question_four_radio_three | question_four_radio_four: 
      checkedTextNotAllCorrect += " 4"; 
    } 

} 

/** 
* Updates the global variable questionFive if the answer was correct 
* Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct 
*/ 
public void answerFive() { 
    RadioGroup questionFiveRadioGroup = (RadioGroup) findViewById(R.id.question_five_radio_group); 
    int radioButtonID = questionFiveRadioGroup.getCheckedRadioButtonId(); 

    switch (radioButtonID) { 
     case R.id.question_five_radio_one: 
      checkedTextNotAllCorrect += " 5"; 

     case R.id.question_five_radio_two: 
      questionFive = true; 

    } 

} 

}

回答

0

對於方法answerTwo()answerFour()answerFive()您正在使用switch-case沒有break聲明是造成這個問題。

添加break聲明後爲每個case爲交換機。請參閱下面的示例answerTwo()

public void answerTwo() { 
RadioGroup questionTwoRadioGroup = (RadioGroup) findViewById(R.id.question_two_radio_group); 
int radioButtonID = questionTwoRadioGroup.getCheckedRadioButtonId(); 

switch (radioButtonID) { 
    case R.id.question_two_radio_one | question_two_radio_three: 
     checkedTextNotAllCorrect += " 2"; 
     break; 
    case R.id.question_two_radio_two: 
     questionTwo = true; 
     break; 
} 

}

執行相同的將你所使用的switch-case其他方法。

+0

謝謝。現在,我剛纔說的麪包消息按下按鈕,覈對答案後挺大每次的問題。我添加了'checkedTextNotAllCorrect =「有些東西是錯誤的,檢查問題編號:」;'到checkAnswersMethod將句子重置爲默認值,然後通過方法更新。但是這不起作用,爲什麼? – gripfly

+0

您需要重設串一次敬酒顯示 – Sanjeet

+0

加上'checkedTextNotAllCorrect =「有些事情不對,檢查問題NR:」'如果其他在'checkAnswersMethod()'方法 – Sanjeet