如何在单选按钮android中计算正确和错误的答案

时间:2017-03-15 11:02:22

标签: android

我是初学Android开发者 我使用单选按钮组进行测验应用我需要计算正确和错误的答案 但它会计算每个我选中的单选按钮。我不知道怎么 ?。

public class MainActivity extends AppCompatActivity {
    /**
     * Global Variable for count Correct answer
     */
   int count_correct_answer =0;
    /**
     *    Global Variable for count worng answer
     */
    int count_wrong_answer=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

public void rdaio_click_group1(View view) {
    RadioGroup radio=(RadioGroup) findViewById(R.id.radio_group1);


    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();


    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_button1:
            if (checked)

            break;
        case R.id.radio_button2:
            if (checked)
                count_correct_answer++;
            radio.clearCheck();
            break;
        case R.id.radio_button3 :
            if(checked)
            break;
    }

}

public void rdaio_click_group2(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_button12:
            if (checked)
                count_correct_answer++;
                break;
        case R.id.radio_button22:
            if (checked)

            break;
        case R.id.radio_button32 :
            if(checked)
                break;
    }

}

4 个答案:

答案 0 :(得分:0)

创建一个按钮以确定答案并控制下一个问题(错误或正确答案)。

此按钮需要调用onClickListener。

答案 1 :(得分:0)

全球宣布:

RadioGroup radio;

on onCreate:

radio = (RadioGroup) findViewById(R.id.radio_group1);

在onClick或任何动作事件中:

radioButtonID = radio.getCheckedRadioButtonId();
View radioButton = radio.findViewById(radioButtonID);
if(radioButton.getId() == R.id.radio_button12){
   count_correct_answer++;
}
else{
   count_wrong_answer++;
}

据我所知,你想重置radioButton选择。所以,我添加了这些代码来重置radioGroups和count_correct_answer,count_wrong_answer:

submit_answer(View view)显示成功/失败的敬酒后添加以下代码:

radio_groub1_button.clearCheck();
radio_groub2_button.clearCheck();
.
.
.
radio_groub8_button.clearCheck();

count_correct_answer = 0;
count_wrong_answer = 0;

希望这有帮助。

答案 2 :(得分:0)

您提供的代码中存在多个问题。

首先,首先检查您在xml中用于onClick方法的拼写。您已定义public void *rdaio_click_group<number>* ,只需确保在xml和java中拼写相同。

其次,您应该始终在onCreate方法或片段的onCreateView方法中初始化视图和内容。因此,在onCreate之外声明RadioGroup radioGroup;以使其成为全局变量,然后在onCreate中输入

radioGroup = (RadioGroup) findViewById(R.id.radio_group1);

switch部分中,您自己和应用程序对于您想要做什么感到困惑。在

    switch(view.getId()) {
        case R.id.radio_button1:
            if (checked)


            break;
     ......
     }

此时,应用程序知道如果view.getId()的计算结果为true,则应该尝试if条件,然后,仅当布尔值checked求值为true时,switch语句应该会中断。

如果在之后没有括号({}括号)切换条件和循环,那么编译器将假定所有代码直到下一个&#39 ;;&#39; (分号)是您想要包括在括号中而不是其他内容。现在,你在if条件之后留下了一个空行,可能是说当这个条件成立时你不想发生任何事情,编译器也不知道这一点。因此,默认情况下,它包含break条件中的if语句,这是因为if条件之后的下一个分号包含break语句。 / p>

Android工作室不允许这样做,如果您不想对if条件执行任何操作,请不要使用它或在其后面放置空括号,以便Android Studio知道您不想做任何事情条件为真,你必须总是为每个case都有一个break语句,而你的break语句只对那些条件计算为true时的实例,当条件为假时app应该做什么?

它将继续尝试下一个案例,直到它到达default,现在如果以前尝试过的案例中的任何一个评估为true并且具有可到达的break语句,然后,应用程序将执行两个案例(原始案件和新案件)要求执行的所有任务。否则,应用程序将简单地实现default情况并产生错误的输出。

答案 3 :(得分:0)

    /**
 * declare object variable for each groupe button
 */
RadioGroup  radio_groub1_button,radio_groub2_button, radio_groub3_button   , radio_groub4_button;
RadioGroup  radio_groub5_button , radio_groub6_button ,radio_groub7_button , radio_groub8_button ;

/**
 * Global Variable for count Correct answer
 */

int count_correct_answer = 0;     / **      *全局变量计数回答      * /     int count_wrong_answer = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /**
     * find ID for group button 1 to group button 8
     */
    radio_groub1_button = (RadioGroup)findViewById(R.id.radio_group1) ;
    radio_groub2_button = (RadioGroup)findViewById(R.id.radio_group2) ;
    radio_groub3_button = (RadioGroup)findViewById(R.id.radio_group3) ;
    radio_groub4_button = (RadioGroup)findViewById(R.id.radio_group4) ;
    radio_groub5_button = (RadioGroup)findViewById(R.id.radio_group5) ;
    radio_groub6_button = (RadioGroup)findViewById(R.id.radio_group6) ;
    radio_groub7_button = (RadioGroup)findViewById(R.id.radio_group7) ;
    radio_groub8_button = (RadioGroup)findViewById(R.id.radio_group8) ;

}

public void submit_answer(View view){
    int selectteed1 = radio_groub1_button.getCheckedRadioButtonId();
    RadioButton radio_button1 = (RadioButton)findViewById(selectteed1);
    String button1= radio_button1.getText().toString();
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g1= R.id.radio_button2;

    if(selectteed1 == button_g1)  {
       // Toast.makeText(getBaseContext(),"Yes", Toast.LENGTH_LONG).show();
        count_correct_answer++;
    }     else {count_wrong_answer++;}

    /**
     * cheked radio button  that  selectted in group1 and get id .
     * */
   int selectteed2 = radio_groub2_button.getCheckedRadioButtonId();
    RadioButton radio_button2 = (RadioButton)findViewById(selectteed2);
    String button2 =radio_button2.getText().toString();
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g2= R.id.radio_button12;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed2== button_g2)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}

    int selectteed3 = radio_groub3_button.getCheckedRadioButtonId();
    RadioButton radio_button3 = (RadioButton)findViewById(selectteed3);
    String button3 = radio_button3.getText().toString();
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g3= R.id.radio_button33;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed3== button_g3)  {

        count_correct_answer++;
    } else {count_wrong_answer++;}

    /**
     * cheked radio button  that  selectted in group1 and get id .
     */
    int selectteed4 = radio_groub4_button.getCheckedRadioButtonId();
    RadioButton radio_button4 = (RadioButton)findViewById(selectteed4);
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g4= R.id.radio_button14;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed4== button_g4)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}

    /**
     * cheked radio button  that  selectted in group1 and get id .
     */
    int selectteed5 = radio_groub5_button.getCheckedRadioButtonId();
    RadioButton radio_button5 = (RadioButton)findViewById(selectteed5);
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g5= R.id.radio_button25;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed5== button_g5)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}

    /**
     * cheked radio button  that  selectted in group1 and get id .
     */
    int selectteed6 = radio_groub6_button.getCheckedRadioButtonId();
    RadioButton radio_button6 = (RadioButton)findViewById(selectteed6);
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g6= R.id.radio_button26;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed6== button_g6)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}

    /**
     * cheked radio button  that  selectted in group1 and get id .
     */
    int selectteed7 = radio_groub7_button.getCheckedRadioButtonId();
    RadioButton radio_button7 = (RadioButton)findViewById(selectteed7);
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g7= R.id.radio_button27;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed7== button_g7)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}
   /**
    * cheked radio button  that  selectted in group1 and get id .
    */
    int selectteed8 = radio_groub8_button.getCheckedRadioButtonId();
    RadioButton radio_button8 = (RadioButton)findViewById(selectteed8);
    /**
     * get id of correct answer and store in variable.
     */
    int  button_g8= R.id.radio_button28;
    /**
     * check  for correct answer , wrong  and count it .
     */
    if(selectteed8== button_g8)  {

        count_correct_answer++;
    }
    else {count_wrong_answer++;}

    String succeed  = "Number of correct answer is \t "+ count_correct_answer + "\n you are done";
    String faild = "Number of wrong answer is \t "+ count_wrong_answer + "\n you are faild";
        if(count_correct_answer >= count_wrong_answer){
            Toast.makeText(getBaseContext(),succeed, Toast.LENGTH_LONG).show();
    }
   else{Toast.makeText(getBaseContext(),faild, Toast.LENGTH_LONG).show();}






}

XML

<LinearLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/Q_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q1" />

    <RadioGroup
        android:id="@+id/radio_group1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <RadioButton
            android:id="@+id/radio_button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button2" />

        <RadioButton
            android:id="@+id/radio_button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="@string/button3" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q2" />

    <RadioGroup
        android:id="@+id/radio_group2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button12" />

        <RadioButton
            android:id="@+id/radio_button22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button22" />

        <RadioButton
            android:id="@+id/radio_button32"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button32" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q3" />

    <RadioGroup
        android:id="@+id/radio_group3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button13" />

        <RadioButton
            android:id="@+id/radio_button23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button23" />

        <RadioButton
            android:id="@+id/radio_button33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button33" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q4" />

    <RadioGroup
        android:id="@+id/radio_group4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button14" />

        <RadioButton
            android:id="@+id/radio_button24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button24" />

        <RadioButton
            android:id="@+id/radio_button34"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button34" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q5" />

    <RadioGroup
        android:id="@+id/radio_group5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button15" />

        <RadioButton
            android:id="@+id/radio_button25"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button25" />

        <RadioButton
            android:id="@+id/radio_button35"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button35" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q6" />

    <RadioGroup
        android:id="@+id/radio_group6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button16" />

        <RadioButton
            android:id="@+id/radio_button26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button26" />

        <RadioButton
            android:id="@+id/radio_button36"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button36" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q7" />

    <RadioGroup
        android:id="@+id/radio_group7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button17" />

        <RadioButton
            android:id="@+id/radio_button27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button27" />

        <RadioButton
            android:id="@+id/radio_button37"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button37" />
    </RadioGroup>

    <TextView
        android:id="@+id/Q_8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:text="@string/Q8" />

    <RadioGroup
        android:id="@+id/radio_group8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_button18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button18" />

        <RadioButton
            android:id="@+id/radio_button28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button28" />

        <RadioButton
            android:id="@+id/radio_button38"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button38" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:onClick="submit_answer"
        android:text="@string/submit"/>

    <TextView
        android:id="@+id/show_answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:layout_marginTop="12dp"
        android:padding="23dp"
        android:text="" />`enter code here`