【发布时间】: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)));上方或下方