英文:
How to calculate score using Radio buttons in Android?
问题
以下是你提供的代码的翻译部分:
这是 Activity 类:
public class CoughTest extends AppCompatActivity {
int score = 0;
RadioGroup rg1, rg2, rg3, rg4, rg5;
RadioButton rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
rg1 = (RadioGroup) findViewById(R.id.rgroup1);
rg2 = (RadioGroup) findViewById(R.id.rgroup2);
rg3 = (RadioGroup) findViewById(R.id.rgroup3);
rg4 = (RadioGroup) findViewById(R.id.rgroup4);
rg5 = (RadioGroup) findViewById(R.id.rgroup5);
}
public void rbClicked(View view) {
int radioButtonId = rg1.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(radioButtonId);
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.yes1:
score = score + 20;
break;
case R.id.no1:
score = 0;
break;
case R.id.yes2:
score = score + 20;
break;
case R.id.no2:
score = 0;
break;
case R.id.yes3:
score = score + 20;
break;
case R.id.no3:
score = 0;
break;
case R.id.yes4:
score = score + 20;
break;
case R.id.no4:
score = 0;
break;
case R.id.yes5:
score = score + 20;
break;
case R.id.no5:
score = 0;
break;
}
}
}
这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have a cough?"
android:textColor="#100D40"
android:textSize="20sp" />
<!-- 其他部分省略 -->
</LinearLayout>
</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
public class CoughTest extends AppCompatActivity {
int score= 0;
RadioGroup rg1,rg2,rg3,rg4,rg5;
RadioButton rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
rg1 = (RadioGroup) findViewById(R.id.rgroup1);
rg2 = (RadioGroup) findViewById(R.id.rgroup2);
rg3 = (RadioGroup) findViewById(R.id.rgroup3);
rg4 = (RadioGroup) findViewById(R.id.rgroup4);
rg5 = (RadioGroup) findViewById(R.id.rgroup5);
}
public void rbClicked(View view) {
int radioButtonId = rg1.getCheckedRadioButtonId();
rb =(RadioButton) findViewById(radioButtonId);
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.yes1:
score = score + 20;
break;
case R.id.no1:
score = 0;
break;
case R.id.yes2:
score = score + 20;
break;
case R.id.no2:
score = 0;
break;
case R.id.yes3:
score = score + 20;
break;
case R.id.no3:
score = 0;
break;
case R.id.yes4:
score = score + 20;
break;
case R.id.no4:
score = 0;
break;
case R.id.yes5:
score = score + 20;
break;
case R.id.no5:
score = 0;
break;
}
}
}
This is the XML file
activity_cough_test.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have a cough?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgroup1">
<RadioButton
android:id="@+id/yes1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/no1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="@+id/soar_throat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have a soar throat?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgroup2">
<RadioButton
android:id="@+id/yes2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/no2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="@+id/fever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have fever?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgroup3">
<RadioButton
android:id="@+id/yes3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/no3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="@+id/tiredness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have tiredness?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgroup4">
<RadioButton
android:id="@+id/yes4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/no4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="@+id/breathing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Do you have difficulty in breathing?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgroup5">
<RadioButton
android:id="@+id/yes5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/no5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
</LinearLayout>
</ScrollView>
答案1
得分: 1
我将建议这种方法:
CoughTest.java:
public class CoughTest extends AppCompatActivity {
private Button btnSubmit, btnReset;
private TextView finalScore;
private RadioButton cough, sourThroat, fever, tiredness, breathing;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnReset = (Button) findViewById(R.id.btnReset);
finalScore = (TextView) findViewById(R.id.textFinalScore);
cough = (RadioButton) findViewById(R.id.cough);
sourThroat = (RadioButton) findViewById(R.id.sour_throat);
fever = (RadioButton) findViewById(R.id.fever);
tiredness = (RadioButton) findViewById(R.id.tiredness);
breathing = (RadioButton) findViewById(R.id.breathingDifficulty);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cough.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (sourThroat.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (fever.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (tiredness.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (breathing.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
// 最后显示您的得分。
Toast.makeText(getApplicationContext(), "得分是:" + score, Toast.LENGTH_SHORT).show();
}
});
// 重置所有内容
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
score = 0;
cough.setChecked(false);
fever.setChecked(false);
breathing.setChecked(false);
tiredness.setChecked(false);
sourThroat.setChecked(false);
}
});
}
}
回到你的XML:我进行了修改。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="请勾选(✓)如果您有这些症状。"
android:textColor="#100D40"
android:textSize="16sp" />
<RadioButton
android:id="@+id/cough"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="咳嗽"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/sour_throat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="喉咙痛"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/fever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="发热"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/tiredness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="疲劳"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/breathingDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="呼吸困难"
android:textColor="#100D40" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/btnSubmit"
android:layout_marginStart="50sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:layout_marginTop="5sp"
android:id="@+id/btnReset"
android:layout_marginStart="50sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_marginStart="20sp"
android:hint="您的分数在这里"
android:textColor="#000000"
android:id="@+id/textFinalScore"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我已删除了单选按钮组。只需提示用户在同意时单击它。这意味着如果他们的答案是“是”,请勾选单选按钮,否则保持未选中。希望这有所帮助。
英文:
I will suggest this method:
CoughTest.java:
public class CoughTest extends AppCompatActivity {
private Button btnSubmit, btnReset;
private TextView finalScore;
private RadioButton cough, sourThroat, fever, tiredness, breathing;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnReset = (Button) findViewById(R.id.btnReset);
finalScore = (TextView) findViewById(R.id.textFinalScore);
cough = (RadioButton) findViewById(R.id.cough);
sourThroat = (RadioButton) findViewById(R.id.sour_throat);
fever = (RadioButton) findViewById(R.id.fever);
tiredness = (RadioButton) findViewById(R.id.tiredness);
breathing = (RadioButton) findViewById(R.id.breathingDifficulty);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cough.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (sourThroat.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (fever.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (tiredness.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (breathing.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
//Finally show your score.
Toast.makeText(getApplicationContext(), "Score is: " + score, Toast.LENGTH_SHORT).show();
}
});
//Resetting everything
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
score = 0;
cough.setChecked(false);
fever.setChecked(false);
breathing.setChecked(false);
tiredness.setChecked(false);
sourThroat.setChecked(false);
}
});
}
}
Coming back to your XML: I modified it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Please check (✓) if you show the symptom."
android:textColor="#100D40"
android:textSize="16sp" />
<RadioButton
android:id="@+id/cough"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="cough"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/sour_throat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Sour Throat"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/fever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Fever"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/tiredness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="tiredness"
android:textColor="#100D40" />
<RadioButton
android:id="@+id/breathingDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_bold"
android:text="Breathing Difficulty"
android:textColor="#100D40" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="@+id/btnSubmit"
android:layout_marginStart="50sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_marginTop="5sp"
android:id="@+id/btnReset"
android:layout_marginStart="50sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_marginStart="20sp"
android:hint="Your Score Here"
android:textColor="#000000"
android:id="@+id/textFinalScore"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
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.
答案2
得分: 0
只需在您的switch case之后将分数设置到您的textView中:
yourTextView.setText(String.valueOf(score));
英文:
just set score into your textView after your switch case :
yourTextView = String.ValueOf(score);
专注分享java语言的经验与见解,让所有开发者获益!
评论