0

我正在尝试设置一个测验,其中每个答案都会在总分中增加一定数量的分数。目前,我关心单选按钮。我已经建立了一个单选组,我希望选择任何单选按钮来添加到总分中。这只是我的程序的一部分,只是要注意。

当我按下 xml 布局文件底部的按钮时,我希望将与单选按钮关联的分数添加到总分中。你明白吗?

这是实际 xml 布局文件的类文件:

Public class QuestionOne extends Results {

    RadioButton answer1, answer2, answer3;
    Button oneNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);

        RadioButton answer1 = (RadioButton) findViewById(R.id.oneradio1);
        RadioButton answer2 = (RadioButton) findViewById(R.id.oneradio2);
        RadioButton answer3 = (RadioButton) findViewById(R.id.oneradio3);

        Button oneNext = (Button) findViewById(R.id.onenext);


    }

}

它扩展了 Results 类,我这样做是为了继承分数整数。这是分数等级:

public class Results extends Activity {

    private int score;

    public int getScore() {
        return score;
    }

    public Results(int score) {
        this.score = score;
    }

}

我从谷歌上得到了这个结构,我敢肯定它的有效性值得怀疑,但我认为我有基本的想法。谁能帮我吗?

4

1 回答 1

1

声明一个RadioGroup而不是声明单个 RadioButtons。

answerGroup = (RadioGroup) findViewById(R.id.radioGroupAnswer);

您可以获得在 RadioGroup 中选择的按钮的索引。

int index = answerGroup.indexOfChild(findViewById(answerGroup.getCheckedRadioButtonId()));

然后您可以使用此索引值并将分数添加到总分中。

switch(index)
{
case 0 : totalScore = totalScore + x; // x is the score of the first answer
break;
case 1 : totalScore = totalScore + y; // y is the score of the second answer
break;
case 2 : totalScore = totalScore + z; // z is the score of the third answer
break;
}
于 2012-09-03T18:40:46.447 回答