我正在尝试设置一个测验,每个答案会在总分中添加一定数量的分数。目前,我关注单选按钮。我已经设置了一个radiogroup,我希望选择任何单选按钮添加到总分。这只是我的计划的一部分,仅供注意。
当我按下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;
}
}
我从谷歌搜索得到了这个结构,它的有效性值得怀疑我确定,但我认为我有基本的想法。任何人都可以帮助我吗?
答案 0 :(得分: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;
}