从 RadioButton 计算分数
Counting score from RadioButton
如何计算单选按钮的分数?
我有收音机 1、收音机 7、收音机 8、收音机 14。每个radiobutton存放在不同的radioGroup中,radio1在radioGroup1中,radio7在radioGroup2中,radio8在radioGroup3中。
我尝试使用 if 语句但效率不高,我尝试使用 switch 语句但无法计算我的分数
这是我的 if 语句:
RadioButton kunci1 = (RadioButton) findViewById(R.id.radio1);
RadioButton kunci2 = (RadioButton) findViewById(R.id.radio7);
RadioButton kunci3 = (RadioButton) findViewById(R.id.radio8);
RadioButton kunci4 = (RadioButton) findViewById(R.id.radio14);
int score=0;
if (kunci.isChecked() && kunci2.isChecked() && kunci3.isChecked() && kunci4.isChecked()){
score = 100;
} else if ((kunci1.isChecked() && kunci2.isChecked() && kunci3.isChecked())......)
score=75;
这是我的 switch 语句(nilai 是印度尼西亚语的分数):
int nilai=25;
switch(nilai) {
case R.id.radio1:
if (kunci1.isChecked())
nilai++;
case R.id.radio7:
if (kunci2.isChecked())
nilai++;
case R.id.radio8:
if (kunci3.isChecked())
nilai++;
case R.id.radio14:
if (kunci4.isChecked())
nilai++;
default :
break;
}
这是我的布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="504dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="18dp"
android:text="1. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. ITB" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. IPB" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. UPB" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. IPI" />
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="22dp"
android:text="2. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2" >
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Institut Pertanian Bandung" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Institut Pertanian Bali" />
<RadioButton
android:id="@+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Institut Perbankan Bogor" />
<RadioButton
android:id="@+id/radio7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Institut Pertanian Bogor" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup2"
android:layout_below="@+id/radioGroup2"
android:layout_marginTop="20dp"
android:text="3. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3" >
<RadioButton
android:id="@+id/radio8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Laksmi Irianti" />
<RadioButton
android:id="@+id/radio9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Henni" />
<RadioButton
android:id="@+id/radio10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Yeni" />
<RadioButton
android:id="@+id/radio11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Inna Novianti" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup3"
android:layout_below="@+id/radioGroup3"
android:layout_marginTop="20dp"
android:text="4. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4" >
<RadioButton
android:id="@+id/radio12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Komunikasi" />
<RadioButton
android:id="@+id/radio13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. PPP" />
<RadioButton
android:id="@+id/radio14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Tekom" />
<RadioButton
android:id="@+id/radio15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. PKS" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup4"
android:text="Submit"
android:onClick="submit"/>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Timer" />
</RelativeLayout>
您可以执行一些操作,例如维护单选按钮数组并遍历它们。
ArrayList<RadioButton> buttons = new ArrayList<>();
buttons.add((RadioButton) findViewById(R.id.radio1));
buttons.add((RadioButton) findViewById(R.id.radio7));
buttons.add((RadioButton) findViewById(R.id.radio8));
buttons.add((RadioButton) findViewById(R.id.radio14));
for(RadioButton button : buttons){
if (button.isChecked()){
nilai+= 25;
}
}
如果你还想保留原来的按钮,那么你可以使用三元运算符,但它的可读性会降低:
nilai += kunci.isChecked() ? 25 : 0 + kunci2.isChecked() ? 25 : 0 +
kunci3.isChecked() ? 25 : 0 + kunci4.isChecked() ? 25 : 0 ;
你的问题比你想象的要简单,你只需要几个顺序独立的 if 语句(当然也可以是三元运算符)来更新你的分数,例如
int score=0;
if (kunci1.isChecked()) score += 25;
if (kunci2.isChecked()) score += 25;
if (kunci3.isChecked()) score += 25;
if (kunci4.isChecked()) score += 25;
//...
或单线
int score = 25 * ((kunci1.isChecked() ? 1 : 0) + (kunci2.isChecked() ? 1 : 0) + ...)
如何计算单选按钮的分数?
我有收音机 1、收音机 7、收音机 8、收音机 14。每个radiobutton存放在不同的radioGroup中,radio1在radioGroup1中,radio7在radioGroup2中,radio8在radioGroup3中。 我尝试使用 if 语句但效率不高,我尝试使用 switch 语句但无法计算我的分数
这是我的 if 语句:
RadioButton kunci1 = (RadioButton) findViewById(R.id.radio1);
RadioButton kunci2 = (RadioButton) findViewById(R.id.radio7);
RadioButton kunci3 = (RadioButton) findViewById(R.id.radio8);
RadioButton kunci4 = (RadioButton) findViewById(R.id.radio14);
int score=0;
if (kunci.isChecked() && kunci2.isChecked() && kunci3.isChecked() && kunci4.isChecked()){
score = 100;
} else if ((kunci1.isChecked() && kunci2.isChecked() && kunci3.isChecked())......)
score=75;
这是我的 switch 语句(nilai 是印度尼西亚语的分数):
int nilai=25;
switch(nilai) {
case R.id.radio1:
if (kunci1.isChecked())
nilai++;
case R.id.radio7:
if (kunci2.isChecked())
nilai++;
case R.id.radio8:
if (kunci3.isChecked())
nilai++;
case R.id.radio14:
if (kunci4.isChecked())
nilai++;
default :
break;
}
这是我的布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="504dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="18dp"
android:text="1. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. ITB" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. IPB" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. UPB" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. IPI" />
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="22dp"
android:text="2. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2" >
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Institut Pertanian Bandung" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Institut Pertanian Bali" />
<RadioButton
android:id="@+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Institut Perbankan Bogor" />
<RadioButton
android:id="@+id/radio7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Institut Pertanian Bogor" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup2"
android:layout_below="@+id/radioGroup2"
android:layout_marginTop="20dp"
android:text="3. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3" >
<RadioButton
android:id="@+id/radio8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Laksmi Irianti" />
<RadioButton
android:id="@+id/radio9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Henni" />
<RadioButton
android:id="@+id/radio10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Yeni" />
<RadioButton
android:id="@+id/radio11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Inna Novianti" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup3"
android:layout_below="@+id/radioGroup3"
android:layout_marginTop="20dp"
android:text="4. Can't fetch from server" />
<RadioGroup
android:id="@+id/radioGroup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4" >
<RadioButton
android:id="@+id/radio12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="A. Komunikasi" />
<RadioButton
android:id="@+id/radio13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. PPP" />
<RadioButton
android:id="@+id/radio14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Tekom" />
<RadioButton
android:id="@+id/radio15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. PKS" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup4"
android:text="Submit"
android:onClick="submit"/>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Timer" />
</RelativeLayout>
您可以执行一些操作,例如维护单选按钮数组并遍历它们。
ArrayList<RadioButton> buttons = new ArrayList<>();
buttons.add((RadioButton) findViewById(R.id.radio1));
buttons.add((RadioButton) findViewById(R.id.radio7));
buttons.add((RadioButton) findViewById(R.id.radio8));
buttons.add((RadioButton) findViewById(R.id.radio14));
for(RadioButton button : buttons){
if (button.isChecked()){
nilai+= 25;
}
}
如果你还想保留原来的按钮,那么你可以使用三元运算符,但它的可读性会降低:
nilai += kunci.isChecked() ? 25 : 0 + kunci2.isChecked() ? 25 : 0 +
kunci3.isChecked() ? 25 : 0 + kunci4.isChecked() ? 25 : 0 ;
你的问题比你想象的要简单,你只需要几个顺序独立的 if 语句(当然也可以是三元运算符)来更新你的分数,例如
int score=0;
if (kunci1.isChecked()) score += 25;
if (kunci2.isChecked()) score += 25;
if (kunci3.isChecked()) score += 25;
if (kunci4.isChecked()) score += 25;
//...
或单线
int score = 25 * ((kunci1.isChecked() ? 1 : 0) + (kunci2.isChecked() ? 1 : 0) + ...)