【问题标题】:Reset Buttons shows 0 score but doesn't really clean the answers重置按钮显示 0 分,但并没有真正清除答案
【发布时间】:2016-11-14 15:24:30
【问题描述】:

我有一个简单的测验应用程序,用户可以在其中从 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>

【问题讨论】:

  • 您必须取消选中同一 onClick 事件中的所有单选按钮
  • 你声明数据类型是int
  • 大家好,非常感谢您的回答。有人可以告诉我应该在哪里写什么吗?我感到失落......
  • 就在您的submitButton.setText((String.valueOf(0)));上方或下方

标签: java android


【解决方案1】:

RadioGroup.clearCheck();

到你的 resetButton onClick

这可能会对你有所帮助。

【讨论】:

    【解决方案2】:

    做这样的事情

       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();
    
            }
        });
      }
    

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

    希望这会有所帮助!

    【讨论】:

    • 您好,感谢您的回答。我这样做了,但 radioButtons 仍然不干净。结果仍然是用户选择的。
    • radioGroup = (RadioGroup)findViewById(R.id.firstGroupRadioButtons); //resetButton 将分数重置为 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();
    • 如何才能让用户从头开始每个问题和测验再次干净?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多