简体   繁体   English

使用单选按钮在 Android 中制作测验应用程序

[英]Making Quiz App in Android with Radio Buttons

I'm making a Quizz App for Android with 10 Questions, all of them with 4 Radio Buttons, and one button at the end to show the score.我正在制作一个包含 10 个问题的 Android 测验应用程序,所有这些问题都有 4 个单选按钮,最后有一个按钮来显示分数。 The problem is when I choose the correct answer it gives 5 points, but if I check another radio button the points will stay 5 and if I press again it sums 5. What is the best way to code this?问题是,当我选择正确答案时,它会给出 5 分,但是如果我选中另一个单选按钮,则该点将保持为 5,如果我再次按下它,则总和为 5。对此进行编码的最佳方法是什么?

Here is the code:这是代码:

package com.example.android.quizproject;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int points = 0;

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

public void firstRadioButtons (View view){
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()) {
        case R.id.questionOneA:
            if (checked)
                points += 0;
                break;
        case R.id.questionOneB:
            if (checked)
                points += 0;
                break;
        case R.id.questionOneC:
            if (checked)
                points += 5;
                break;
        case R.id.questionOneD:
            if (checked)
                points += 0;
                break;
    }

}

public void showScore (View view) {
    TextView scoreTextView = (TextView) findViewById(R.id.score);
    scoreTextView.setText(" " + points);
}

} }

You can make use of a counter vvariable which checks if the question has been previousy answered or not.您可以使用计数器变量来检查问题是否已被预先回答。 Modify part of your code to this将您的部分代码修改为此

public void firstRadioButtons (View view){
    boolean checked = ((RadioButton) view).isChecked();
    int count=0;
    switch (view.getId()) {
        case R.id.questionOneA:
            if (checked)
                {
            if(count!=0){
                points-=5;
                count=0;
                }
                }
                break;
        case R.id.questionOneB:
            if (checked)
                {
            if(count!=0){
                points-=5;
                count=0;
                }
                }
                break;
        case R.id.questionOneC:
            if (checked){
                points += 5;
                count+=1;}
                break;
        case R.id.questionOneD:
            if (checked)
                {
            if(count!=0){
                points-=5;
                count=0;
                }
                }
                break;
    }

}

Actually, the way you described it, it's common sense.事实上,你描述的方式,这是常识。 If you click the right answer once, it will set it to 5, but if you press any other it will add 0 to it.如果您单击一次正确的答案,它会将其设置为 5,但如果您按任何其他答案,则会将其添加为 0。

In general, it will print out 5 since you got the answer correct once, and the other questions are set to 0. There's really nothing to fix here, it's kind of common sense that your variable wouldn't read other than 5. Just like Abhriya said, you could add a counter increment value as done in ( her / his ) example.一般来说,它会打印出 5,因为你得到了一次正确的答案,其他问题被设置为 0。这里真的没有什么可以解决的,你的变量除了 5 之外不会读取是一种常识。就像Abhriya 说,您可以像 (her/his) 示例中那样添加计数器增量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM