如何在Android中使用单选按钮计算分数?

huangapple go评论126阅读模式
英文:

How to calculate score using Radio buttons in Android?

问题

以下是你提供的代码的翻译部分:

这是 Activity 类:

  1. public class CoughTest extends AppCompatActivity {
  2. int score = 0;
  3. RadioGroup rg1, rg2, rg3, rg4, rg5;
  4. RadioButton rb;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_cough_test);
  9. rg1 = (RadioGroup) findViewById(R.id.rgroup1);
  10. rg2 = (RadioGroup) findViewById(R.id.rgroup2);
  11. rg3 = (RadioGroup) findViewById(R.id.rgroup3);
  12. rg4 = (RadioGroup) findViewById(R.id.rgroup4);
  13. rg5 = (RadioGroup) findViewById(R.id.rgroup5);
  14. }
  15. public void rbClicked(View view) {
  16. int radioButtonId = rg1.getCheckedRadioButtonId();
  17. rb = (RadioButton) findViewById(radioButtonId);
  18. boolean checked = ((RadioButton) view).isChecked();
  19. switch (view.getId()){
  20. case R.id.yes1:
  21. score = score + 20;
  22. break;
  23. case R.id.no1:
  24. score = 0;
  25. break;
  26. case R.id.yes2:
  27. score = score + 20;
  28. break;
  29. case R.id.no2:
  30. score = 0;
  31. break;
  32. case R.id.yes3:
  33. score = score + 20;
  34. break;
  35. case R.id.no3:
  36. score = 0;
  37. break;
  38. case R.id.yes4:
  39. score = score + 20;
  40. break;
  41. case R.id.no4:
  42. score = 0;
  43. break;
  44. case R.id.yes5:
  45. score = score + 20;
  46. break;
  47. case R.id.no5:
  48. score = 0;
  49. break;
  50. }
  51. }
  52. }

这是 XML 文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".CoughTest">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical">
  12. <TextView
  13. android:id="@+id/textView11"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_marginTop="16dp"
  17. android:layout_marginLeft="10dp"
  18. android:fontFamily="@font/roboto_bold"
  19. android:text="Do you have a cough?"
  20. android:textColor="#100D40"
  21. android:textSize="20sp" />
  22. <!-- 其他部分省略 -->
  23. </LinearLayout>
  24. </ScrollView>

请注意,我只翻译了你提供的代码部分,其余内容均被省略。

英文:

I have been developing a form which uses Radio buttons and has a list of questions.

When the user clicks on Yes radio button for every question, I want to increment the score by 20. If the user clicks the No radio button, the score should be 0.

Take a look at the code.

This is the Activity class

CoughTest.java

  1. public class CoughTest extends AppCompatActivity {
  2. int score= 0;
  3. RadioGroup rg1,rg2,rg3,rg4,rg5;
  4. RadioButton rb;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_cough_test);
  9. rg1 = (RadioGroup) findViewById(R.id.rgroup1);
  10. rg2 = (RadioGroup) findViewById(R.id.rgroup2);
  11. rg3 = (RadioGroup) findViewById(R.id.rgroup3);
  12. rg4 = (RadioGroup) findViewById(R.id.rgroup4);
  13. rg5 = (RadioGroup) findViewById(R.id.rgroup5);
  14. }
  15. public void rbClicked(View view) {
  16. int radioButtonId = rg1.getCheckedRadioButtonId();
  17. rb =(RadioButton) findViewById(radioButtonId);
  18. boolean checked = ((RadioButton) view).isChecked();
  19. switch (view.getId()){
  20. case R.id.yes1:
  21. score = score + 20;
  22. break;
  23. case R.id.no1:
  24. score = 0;
  25. break;
  26. case R.id.yes2:
  27. score = score + 20;
  28. break;
  29. case R.id.no2:
  30. score = 0;
  31. break;
  32. case R.id.yes3:
  33. score = score + 20;
  34. break;
  35. case R.id.no3:
  36. score = 0;
  37. break;
  38. case R.id.yes4:
  39. score = score + 20;
  40. break;
  41. case R.id.no4:
  42. score = 0;
  43. break;
  44. case R.id.yes5:
  45. score = score + 20;
  46. break;
  47. case R.id.no5:
  48. score = 0;
  49. break;
  50. }
  51. }
  52. }

This is the XML file

activity_cough_test.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.CoughTest&quot;&gt;
  8. &lt;LinearLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:orientation=&quot;vertical&quot;&gt;
  12. &lt;TextView
  13. android:id=&quot;@+id/textView11&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:layout_marginTop=&quot;16dp&quot;
  17. android:layout_marginLeft=&quot;10dp&quot;
  18. android:fontFamily=&quot;@font/roboto_bold&quot;
  19. android:text=&quot;Do you have a cough?&quot;
  20. android:textColor=&quot;#100D40&quot;
  21. android:textSize=&quot;20sp&quot; /&gt;
  22. &lt;RadioGroup
  23. android:layout_width=&quot;match_parent&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:id=&quot;@+id/rgroup1&quot;&gt;
  26. &lt;RadioButton
  27. android:id=&quot;@+id/yes1&quot;
  28. android:layout_width=&quot;wrap_content&quot;
  29. android:layout_height=&quot;wrap_content&quot;
  30. android:layout_marginTop=&quot;12dp&quot;
  31. android:layout_marginLeft=&quot;10dp&quot;
  32. android:fontFamily=&quot;@font/roboto_bold&quot;
  33. android:onClick=&quot;rbClicked&quot;
  34. android:text=&quot;Yes&quot;
  35. android:textColor=&quot;#100D40&quot; /&gt;
  36. &lt;RadioButton
  37. android:id=&quot;@+id/no1&quot;
  38. android:layout_width=&quot;wrap_content&quot;
  39. android:layout_height=&quot;wrap_content&quot;
  40. android:fontFamily=&quot;@font/roboto_bold&quot;
  41. android:layout_marginLeft=&quot;10dp&quot;
  42. android:onClick=&quot;rbClicked&quot;
  43. android:text=&quot;No&quot;
  44. android:textColor=&quot;#100D40&quot; /&gt;
  45. &lt;/RadioGroup&gt;
  46. &lt;TextView
  47. android:id=&quot;@+id/soar_throat&quot;
  48. android:layout_width=&quot;wrap_content&quot;
  49. android:layout_height=&quot;wrap_content&quot;
  50. android:layout_marginTop=&quot;20dp&quot;
  51. android:layout_marginLeft=&quot;10dp&quot;
  52. android:fontFamily=&quot;@font/roboto_bold&quot;
  53. android:text=&quot;Do you have a soar throat?&quot;
  54. android:textColor=&quot;#100D40&quot;
  55. android:textSize=&quot;20sp&quot; /&gt;
  56. &lt;RadioGroup
  57. android:layout_width=&quot;match_parent&quot;
  58. android:layout_height=&quot;wrap_content&quot;
  59. android:id=&quot;@+id/rgroup2&quot;&gt;
  60. &lt;RadioButton
  61. android:id=&quot;@+id/yes2&quot;
  62. android:layout_width=&quot;wrap_content&quot;
  63. android:layout_height=&quot;wrap_content&quot;
  64. android:layout_marginTop=&quot;12dp&quot;
  65. android:layout_marginLeft=&quot;10dp&quot;
  66. android:fontFamily=&quot;@font/roboto_bold&quot;
  67. android:onClick=&quot;rbClicked&quot;
  68. android:text=&quot;Yes&quot;
  69. android:textColor=&quot;#100D40&quot; /&gt;
  70. &lt;RadioButton
  71. android:id=&quot;@+id/no2&quot;
  72. android:layout_width=&quot;wrap_content&quot;
  73. android:layout_height=&quot;wrap_content&quot;
  74. android:fontFamily=&quot;@font/roboto_bold&quot;
  75. android:layout_marginLeft=&quot;10dp&quot;
  76. android:onClick=&quot;rbClicked&quot;
  77. android:text=&quot;No&quot;
  78. android:textColor=&quot;#100D40&quot; /&gt;
  79. &lt;/RadioGroup&gt;
  80. &lt;TextView
  81. android:id=&quot;@+id/fever&quot;
  82. android:layout_width=&quot;wrap_content&quot;
  83. android:layout_height=&quot;wrap_content&quot;
  84. android:layout_marginTop=&quot;24dp&quot;
  85. android:layout_marginLeft=&quot;10dp&quot;
  86. android:fontFamily=&quot;@font/roboto_bold&quot;
  87. android:text=&quot;Do you have fever?&quot;
  88. android:textColor=&quot;#100D40&quot;
  89. android:textSize=&quot;20sp&quot; /&gt;
  90. &lt;RadioGroup
  91. android:layout_width=&quot;match_parent&quot;
  92. android:layout_height=&quot;wrap_content&quot;
  93. android:id=&quot;@+id/rgroup3&quot;&gt;
  94. &lt;RadioButton
  95. android:id=&quot;@+id/yes3&quot;
  96. android:layout_width=&quot;wrap_content&quot;
  97. android:layout_height=&quot;wrap_content&quot;
  98. android:layout_marginTop=&quot;12dp&quot;
  99. android:layout_marginLeft=&quot;10dp&quot;
  100. android:fontFamily=&quot;@font/roboto_bold&quot;
  101. android:onClick=&quot;rbClicked&quot;
  102. android:text=&quot;Yes&quot;
  103. android:textColor=&quot;#100D40&quot; /&gt;
  104. &lt;RadioButton
  105. android:id=&quot;@+id/no3&quot;
  106. android:layout_width=&quot;wrap_content&quot;
  107. android:layout_height=&quot;wrap_content&quot;
  108. android:fontFamily=&quot;@font/roboto_bold&quot;
  109. android:layout_marginLeft=&quot;10dp&quot;
  110. android:onClick=&quot;rbClicked&quot;
  111. android:text=&quot;No&quot;
  112. android:textColor=&quot;#100D40&quot; /&gt;
  113. &lt;/RadioGroup&gt;
  114. &lt;TextView
  115. android:id=&quot;@+id/tiredness&quot;
  116. android:layout_width=&quot;wrap_content&quot;
  117. android:layout_height=&quot;wrap_content&quot;
  118. android:layout_marginTop=&quot;24dp&quot;
  119. android:layout_marginLeft=&quot;10dp&quot;
  120. android:fontFamily=&quot;@font/roboto_bold&quot;
  121. android:text=&quot;Do you have tiredness?&quot;
  122. android:textColor=&quot;#100D40&quot;
  123. android:textSize=&quot;20sp&quot; /&gt;
  124. &lt;RadioGroup
  125. android:layout_width=&quot;match_parent&quot;
  126. android:layout_height=&quot;wrap_content&quot;
  127. android:id=&quot;@+id/rgroup4&quot;&gt;
  128. &lt;RadioButton
  129. android:id=&quot;@+id/yes4&quot;
  130. android:layout_width=&quot;wrap_content&quot;
  131. android:layout_height=&quot;wrap_content&quot;
  132. android:layout_marginTop=&quot;12dp&quot;
  133. android:layout_marginLeft=&quot;10dp&quot;
  134. android:fontFamily=&quot;@font/roboto_bold&quot;
  135. android:onClick=&quot;rbClicked&quot;
  136. android:text=&quot;Yes&quot;
  137. android:textColor=&quot;#100D40&quot; /&gt;
  138. &lt;RadioButton
  139. android:id=&quot;@+id/no4&quot;
  140. android:layout_width=&quot;wrap_content&quot;
  141. android:layout_height=&quot;wrap_content&quot;
  142. android:fontFamily=&quot;@font/roboto_bold&quot;
  143. android:layout_marginLeft=&quot;10dp&quot;
  144. android:onClick=&quot;rbClicked&quot;
  145. android:text=&quot;No&quot;
  146. android:textColor=&quot;#100D40&quot; /&gt;
  147. &lt;/RadioGroup&gt;
  148. &lt;TextView
  149. android:id=&quot;@+id/breathing&quot;
  150. android:layout_width=&quot;wrap_content&quot;
  151. android:layout_height=&quot;wrap_content&quot;
  152. android:layout_marginTop=&quot;20dp&quot;
  153. android:layout_marginLeft=&quot;10dp&quot;
  154. android:fontFamily=&quot;@font/roboto_bold&quot;
  155. android:text=&quot;Do you have difficulty in breathing?&quot;
  156. android:textColor=&quot;#100D40&quot;
  157. android:textSize=&quot;20sp&quot; /&gt;
  158. &lt;RadioGroup
  159. android:layout_width=&quot;match_parent&quot;
  160. android:layout_height=&quot;wrap_content&quot;
  161. android:id=&quot;@+id/rgroup5&quot;&gt;
  162. &lt;RadioButton
  163. android:id=&quot;@+id/yes5&quot;
  164. android:layout_width=&quot;wrap_content&quot;
  165. android:layout_height=&quot;wrap_content&quot;
  166. android:layout_marginTop=&quot;12dp&quot;
  167. android:layout_marginLeft=&quot;10dp&quot;
  168. android:fontFamily=&quot;@font/roboto_bold&quot;
  169. android:onClick=&quot;rbClicked&quot;
  170. android:text=&quot;Yes&quot;
  171. android:textColor=&quot;#100D40&quot; /&gt;
  172. &lt;RadioButton
  173. android:id=&quot;@+id/no5&quot;
  174. android:layout_width=&quot;wrap_content&quot;
  175. android:layout_height=&quot;wrap_content&quot;
  176. android:fontFamily=&quot;@font/roboto_bold&quot;
  177. android:layout_marginLeft=&quot;10dp&quot;
  178. android:onClick=&quot;rbClicked&quot;
  179. android:text=&quot;No&quot;
  180. android:textColor=&quot;#100D40&quot; /&gt;
  181. &lt;/RadioGroup&gt;
  182. &lt;/LinearLayout&gt;
  183. &lt;/ScrollView&gt;

答案1

得分: 1

我将建议这种方法:

CoughTest.java:

  1. public class CoughTest extends AppCompatActivity {
  2. private Button btnSubmit, btnReset;
  3. private TextView finalScore;
  4. private RadioButton cough, sourThroat, fever, tiredness, breathing;
  5. private int score = 0;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_cough_test);
  10. btnSubmit = (Button) findViewById(R.id.btnSubmit);
  11. btnReset = (Button) findViewById(R.id.btnReset);
  12. finalScore = (TextView) findViewById(R.id.textFinalScore);
  13. cough = (RadioButton) findViewById(R.id.cough);
  14. sourThroat = (RadioButton) findViewById(R.id.sour_throat);
  15. fever = (RadioButton) findViewById(R.id.fever);
  16. tiredness = (RadioButton) findViewById(R.id.tiredness);
  17. breathing = (RadioButton) findViewById(R.id.breathingDifficulty);
  18. btnSubmit.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. if (cough.isChecked()) {
  22. score = score + 20;
  23. } else {
  24. score = score + 0;
  25. }
  26. if (sourThroat.isChecked()) {
  27. score = score + 20;
  28. } else {
  29. score = score + 0;
  30. }
  31. if (fever.isChecked()) {
  32. score = score + 20;
  33. } else {
  34. score = score + 0;
  35. }
  36. if (tiredness.isChecked()) {
  37. score = score + 20;
  38. } else {
  39. score = score + 0;
  40. }
  41. if (breathing.isChecked()) {
  42. score = score + 20;
  43. } else {
  44. score = score + 0;
  45. }
  46. // 最后显示您的得分。
  47. Toast.makeText(getApplicationContext(), "得分是:" + score, Toast.LENGTH_SHORT).show();
  48. }
  49. });
  50. // 重置所有内容
  51. btnReset.setOnClickListener(new View.OnClickListener() {
  52. @Override
  53. public void onClick(View v) {
  54. score = 0;
  55. cough.setChecked(false);
  56. fever.setChecked(false);
  57. breathing.setChecked(false);
  58. tiredness.setChecked(false);
  59. sourThroat.setChecked(false);
  60. }
  61. });
  62. }
  63. }

回到你的XML:我进行了修改。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".CoughTest">
  9. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  10. xmlns:app="http://schemas.android.com/apk/res-auto"
  11. xmlns:tools="http://schemas.android.com/tools"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. tools:context=".CoughTest">
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical">
  19. <TextView
  20. android:id="@+id/textView12"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_marginTop="16dp"
  24. android:layout_marginLeft="10dp"
  25. android:fontFamily="@font/roboto_bold"
  26. android:text="请勾选(✓)如果您有这些症状。"
  27. android:textColor="#100D40"
  28. android:textSize="16sp" />
  29. <RadioButton
  30. android:id="@+id/cough"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_marginTop="12dp"
  34. android:layout_marginLeft="10dp"
  35. android:fontFamily="@font/roboto_bold"
  36. android:text="咳嗽"
  37. android:textColor="#100D40" />
  38. <RadioButton
  39. android:id="@+id/sour_throat"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_marginTop="12dp"
  43. android:layout_marginLeft="10dp"
  44. android:fontFamily="@font/roboto_bold"
  45. android:text="喉咙痛"
  46. android:textColor="#100D40" />
  47. <RadioButton
  48. android:id="@+id/fever"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="12dp"
  52. android:layout_marginLeft="10dp"
  53. android:fontFamily="@font/roboto_bold"
  54. android:text="发热"
  55. android:textColor="#100D40" />
  56. <RadioButton
  57. android:id="@+id/tiredness"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_marginTop="12dp"
  61. android:layout_marginLeft="10dp"
  62. android:fontFamily="@font/roboto_bold"
  63. android:text="疲劳"
  64. android:textColor="#100D40" />
  65. <RadioButton
  66. android:id="@+id/breathingDifficulty"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:layout_marginTop="12dp"
  70. android:layout_marginLeft="10dp"
  71. android:fontFamily="@font/roboto_bold"
  72. android:text="呼吸困难"
  73. android:textColor="#100D40" />
  74. <Button
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:text="提交"
  78. android:id="@+id/btnSubmit"
  79. android:layout_marginStart="50sp"/>
  80. <Button
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:text="重置"
  84. android:layout_marginTop="5sp"
  85. android:id="@+id/btnReset"
  86. android:layout_marginStart="50sp"/>
  87. <TextView
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:layout_marginTop="20sp"
  91. android:layout_marginStart="20sp"
  92. android:hint="您的分数在这里"
  93. android:textColor="#000000"
  94. android:id="@+id/textFinalScore"/>
  95. </LinearLayout>
  96. </ScrollView>
  97. </RelativeLayout>

我已删除了单选按钮组。只需提示用户在同意时单击它。这意味着如果他们的答案是“是”,请勾选单选按钮,否则保持未选中。希望这有所帮助。

顺便说一下,它的工作原理如下:如何在Android中使用单选按钮计算分数?

英文:

I will suggest this method:

CoughTest.java:

  1. public class CoughTest extends AppCompatActivity {
  2. private Button btnSubmit, btnReset;
  3. private TextView finalScore;
  4. private RadioButton cough, sourThroat, fever, tiredness, breathing;
  5. private int score = 0;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_cough_test);
  10. btnSubmit = (Button) findViewById(R.id.btnSubmit);
  11. btnReset = (Button) findViewById(R.id.btnReset);
  12. finalScore = (TextView) findViewById(R.id.textFinalScore);
  13. cough = (RadioButton) findViewById(R.id.cough);
  14. sourThroat = (RadioButton) findViewById(R.id.sour_throat);
  15. fever = (RadioButton) findViewById(R.id.fever);
  16. tiredness = (RadioButton) findViewById(R.id.tiredness);
  17. breathing = (RadioButton) findViewById(R.id.breathingDifficulty);
  18. btnSubmit.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. if (cough.isChecked()) {
  22. score = score + 20;
  23. } else {
  24. score = score + 0;
  25. }
  26. if (sourThroat.isChecked()) {
  27. score = score + 20;
  28. } else {
  29. score = score + 0;
  30. }
  31. if (fever.isChecked()) {
  32. score = score + 20;
  33. } else {
  34. score = score + 0;
  35. }
  36. if (tiredness.isChecked()) {
  37. score = score + 20;
  38. } else {
  39. score = score + 0;
  40. }
  41. if (breathing.isChecked()) {
  42. score = score + 20;
  43. } else {
  44. score = score + 0;
  45. }
  46. //Finally show your score.
  47. Toast.makeText(getApplicationContext(), &quot;Score is: &quot; + score, Toast.LENGTH_SHORT).show();
  48. }
  49. });
  50. //Resetting everything
  51. btnReset.setOnClickListener(new View.OnClickListener() {
  52. @Override
  53. public void onClick(View v) {
  54. score = 0;
  55. cough.setChecked(false);
  56. fever.setChecked(false);
  57. breathing.setChecked(false);
  58. tiredness.setChecked(false);
  59. sourThroat.setChecked(false);
  60. }
  61. });
  62. }

}

Coming back to your XML: I modified it.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. tools:context=&quot;.CoughTest&quot;&gt;
  9. &lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  10. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  11. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;match_parent&quot;
  14. tools:context=&quot;.CoughTest&quot;&gt;
  15. &lt;LinearLayout
  16. android:layout_width=&quot;match_parent&quot;
  17. android:layout_height=&quot;wrap_content&quot;
  18. android:orientation=&quot;vertical&quot;&gt;
  19. &lt;TextView
  20. android:id=&quot;@+id/textView12&quot;
  21. android:layout_width=&quot;wrap_content&quot;
  22. android:layout_height=&quot;wrap_content&quot;
  23. android:layout_marginTop=&quot;16dp&quot;
  24. android:layout_marginLeft=&quot;10dp&quot;
  25. android:fontFamily=&quot;@font/roboto_bold&quot;
  26. android:text=&quot;Please check (✓) if you show the symptom.&quot;
  27. android:textColor=&quot;#100D40&quot;
  28. android:textSize=&quot;16sp&quot; /&gt;
  29. &lt;RadioButton
  30. android:id=&quot;@+id/cough&quot;
  31. android:layout_width=&quot;wrap_content&quot;
  32. android:layout_height=&quot;wrap_content&quot;
  33. android:layout_marginTop=&quot;12dp&quot;
  34. android:layout_marginLeft=&quot;10dp&quot;
  35. android:fontFamily=&quot;@font/roboto_bold&quot;
  36. android:text=&quot;cough&quot;
  37. android:textColor=&quot;#100D40&quot; /&gt;
  38. &lt;RadioButton
  39. android:id=&quot;@+id/sour_throat&quot;
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:layout_marginTop=&quot;12dp&quot;
  43. android:layout_marginLeft=&quot;10dp&quot;
  44. android:fontFamily=&quot;@font/roboto_bold&quot;
  45. android:text=&quot;Sour Throat&quot;
  46. android:textColor=&quot;#100D40&quot; /&gt;
  47. &lt;RadioButton
  48. android:id=&quot;@+id/fever&quot;
  49. android:layout_width=&quot;wrap_content&quot;
  50. android:layout_height=&quot;wrap_content&quot;
  51. android:layout_marginTop=&quot;12dp&quot;
  52. android:layout_marginLeft=&quot;10dp&quot;
  53. android:fontFamily=&quot;@font/roboto_bold&quot;
  54. android:text=&quot;Fever&quot;
  55. android:textColor=&quot;#100D40&quot; /&gt;
  56. &lt;RadioButton
  57. android:id=&quot;@+id/tiredness&quot;
  58. android:layout_width=&quot;wrap_content&quot;
  59. android:layout_height=&quot;wrap_content&quot;
  60. android:layout_marginTop=&quot;12dp&quot;
  61. android:layout_marginLeft=&quot;10dp&quot;
  62. android:fontFamily=&quot;@font/roboto_bold&quot;
  63. android:text=&quot;tiredness&quot;
  64. android:textColor=&quot;#100D40&quot; /&gt;
  65. &lt;RadioButton
  66. android:id=&quot;@+id/breathingDifficulty&quot;
  67. android:layout_width=&quot;wrap_content&quot;
  68. android:layout_height=&quot;wrap_content&quot;
  69. android:layout_marginTop=&quot;12dp&quot;
  70. android:layout_marginLeft=&quot;10dp&quot;
  71. android:fontFamily=&quot;@font/roboto_bold&quot;
  72. android:text=&quot;Breathing Difficulty&quot;
  73. android:textColor=&quot;#100D40&quot; /&gt;
  74. &lt;Button
  75. android:layout_width=&quot;wrap_content&quot;
  76. android:layout_height=&quot;wrap_content&quot;
  77. android:text=&quot;submit&quot;
  78. android:id=&quot;@+id/btnSubmit&quot;
  79. android:layout_marginStart=&quot;50sp&quot;/&gt;
  80. &lt;Button
  81. android:layout_width=&quot;wrap_content&quot;
  82. android:layout_height=&quot;wrap_content&quot;
  83. android:text=&quot;Reset&quot;
  84. android:layout_marginTop=&quot;5sp&quot;
  85. android:id=&quot;@+id/btnReset&quot;
  86. android:layout_marginStart=&quot;50sp&quot;/&gt;
  87. &lt;TextView
  88. android:layout_width=&quot;wrap_content&quot;
  89. android:layout_height=&quot;wrap_content&quot;
  90. android:layout_marginTop=&quot;20sp&quot;
  91. android:layout_marginStart=&quot;20sp&quot;
  92. android:hint=&quot;Your Score Here&quot;
  93. android:textColor=&quot;#000000&quot;
  94. android:id=&quot;@+id/textFinalScore&quot;/&gt;
  95. &lt;/LinearLayout&gt;
  96. &lt;/ScrollView&gt;
  97. &lt;/RelativeLayout&gt;

I have removed the radio groups. Just prompt your user to click it when they agree.
Means if their answer is YES, then please check the radio button else leave it unchecked.
Hope that helps.

By the way this is how it works:
如何在Android中使用单选按钮计算分数?

答案2

得分: 0

只需在您的switch case之后将分数设置到您的textView中:

  1. yourTextView.setText(String.valueOf(score));
英文:

just set score into your textView after your switch case :

  1. yourTextView = String.ValueOf(score);

huangapple
  • 本文由 发表于 2020年4月6日 12:08:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定