重置按钮显示0分,但并没有真正清理答案

时间:2016-07-12 11:44:01

标签: java android

我有一个简单的测验应用,用户可以从scoreButton查看他们的分数,并从resetButton重置他们的分数。问题出现在RadioBoxes中。问题是,当用户点击resetButton时,按钮显示0但是没有清除用户选择从头开始的radioBoxes。我在代码中遗漏了一些但无法弄明白的东西。     任何帮助都很受欢迎!

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String Name;
    int score = 0;
    Button submitButton;
    Button resetButton;

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

        //user input name
         final EditText nameField = (EditText)findViewById(R.id.nameField);
          Name = nameField.getText().toString();

        //submitButton shows user score
        submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText("Your score is:" + score);
            }
        });
        //resetButton reset score to 0
        resetButton = (Button) findViewById(R.id.resetButton);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText((String.valueOf(0)));
            }
        });
    }
    //This method is called when Radio Buttons are clicked
    public void onRadioButtonClicked(View view) {

        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId()) {
            //display a toast message all right answers
            case R.id.firstLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score ++;
                break;
            case R.id.secondRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.thirdLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.fourthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.fifthRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.sixthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;

            //display a toast message for all wrong answers
            case R.id.firstRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.secondLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.thirdRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.fourthRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.fifthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.sixthRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
        }
    }

activity_main.xml中

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/nameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:inputType="text"
        android:hint="Name"
        android:textColor="#EF6C00"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/welcomeMessage"
        style="@style/WelcomeScreenText"
        android:fontFamily="sans-serif-light"
        android:text="Welcome to Hungry For History!\n         Let's get started!"/>

    <TextView
        android:id="@+id/firstQuestion"
        style="@style/QuestionsStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/welcomeMessage"
        android:paddingLeft="5dp"
        android:text="Who was born in Ancient City Stagira, Greece?"/>

    <TextView
        android:id="@+id/secondQuestion"
        style="@style/QuestionsStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstLeftRadioButton"
        android:paddingLeft="5dp"
        android:text="Who said in his last speech:With malice toward none;...let us strive on to finish the work we are in;to bind up the nation's wounds;into care for him who shall have borne the battle and for his widow and his orphans?"/>

    <TextView
        android:id="@+id/thirdQuestion"
        style="@style/QuestionsStyle"
        android:layout_below="@+id/secondLeftRadioButton"
        android:paddingLeft="5dp"
        android:text="Where the An Lushan Rebellion took place?"/>

    <TextView
        android:id="@+id/fourthQuestion"
        style="@style/QuestionsStyle"
        android:layout_below="@+id/thirdLeftRadioButton"
        android:text="Who was the most famous exemplar of absolute monarchy in France?"/>

    <TextView
        android:id="@+id/fifthQuestion"
        style="@style/QuestionsStyle"
        android:layout_below="@+id/fourthLeftRadioButton"
        android:text="When Alexander The Great lived?"/>

    <TextView
        android:id="@+id/sixthQuestion"
        style="@style/QuestionsStyle"
        android:layout_below="@+id/fifthLeftRadioButton"
        android:text="Where Albert Einstein studied?"/>

    <TextView
        android:id="@+id/seventhQuestion"
        style="@style/QuestionsStyle"
        android:layout_below="@+id/sixthLeftRadioButton"
        android:text="What was the main interest of Democritus?"/>

    <RadioGroup
        android:id="@+id/firstGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/firstLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/firstQuestion"
        android:text="Aristotle"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/firstRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/firstQuestion"
        android:layout_toRightOf="@+id/firstLeftRadioButton"
        android:text="Pythagoras"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/secondGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/secondQuestion"
        android:orientation="horizontal"/>
    <RadioButton
        android:id="@+id/secondLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/secondQuestion"
        android:text="William McKinley"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/secondRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/secondQuestion"
        android:layout_toRightOf="@+id/secondLeftRadioButton"
        android:text="Abraham Lincoln"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/thirdGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thirdQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/thirdLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/thirdQuestion"
        android:text="China"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/thirdRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/thirdQuestion"
        android:layout_toRightOf="@+id/thirdLeftRadioButton"
        android:text="Thailand"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/fourthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/fourthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/fourthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fourthQuestion"
        android:text="Louis XIV"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/fourthRightCheckBox"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fourthQuestion"
        android:layout_toRightOf="@+id/fourthLeftRadioButton"
        android:text="Michael I"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/fifthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/fifthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/fifthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fifthQuestion"
        android:text="330-323 BC"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/fifthRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fifthQuestion"
        android:layout_toRightOf="@+id/fifthLeftRadioButton"
        android:text="336-323 BC"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/sixthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sixthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/sixthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/sixthQuestion"
        android:text="University of Zurich"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/sixthRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/sixthQuestion"
        android:layout_toRightOf="@+id/sixthLeftRadioButton"
        android:text="University of Germany"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/seventhGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/seventhQuestion"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/seventhLeftRadioButton"
            style="@style/RadioButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mathematics-Astronomy"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/seventhRightRadioButton"
            style="@style/RadioButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Philosophy-Psychology"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>


    <Button
        android:id="@+id/submitButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seventhGroupRadioButtons"
        android:layout_marginBottom="3dp"
        android:background="@color/backgroundColor"
        android:text="Submit"
        android:textColor="@color/textColor"
        android:onClick="OnClick"/>

    <Button
        android:id="@+id/resetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/submitButton"
        android:layout_marginBottom="3dp"
        android:onClick="OnClick"
        android:background="@color/backgroundResetColor"
        android:textColor="@color/textColor"
        android:text="Reset"
        android:textAllCaps="true"/>



</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

RadioGroup.clearCheck();

到你的resetButton onClick

这可能会对你有帮助。

答案 1 :(得分:0)

做这样的事情

   RadioGroup radioGroup;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     radioGroup= (RadioGroup)findViewById(R.id.firstGroupRadioButtons);
    //resetButton reset score to 0
    resetButton = (Button) findViewById(R.id.resetButton);
    resetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            submitButton.setText((String.valueOf(0)));
            radioGroup.clearCheck();

        }
    });
  }

您需要声明您的广播组然后定义它!所以你可以清除单选按钮上的所有检查。

希望这有帮助!