2012-09-03 36 views
0

我正在尝试设置一个测验,其中每个答案都会为总分添加一定数量的分数。目前,我关心单选按钮。我已经建立了一个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); 


    } 

} 

它扩展了结果类,我这样做所以它继承了比分整数。以下是评分等级:

public class Results extends Activity { 

    private int score; 

    public int getScore() { 
     return score; 
    } 

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

} 

我得到的结构从谷歌上搜索和它的有效性是值得怀疑的,我知道,但我觉得我有基本的了解。谁能帮我吗?

回答

1

声明RadioGroup而不是声明单个RadioButton。

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; 
} 
+0

怎么样一个新的意图?我可以把它放在案例和断线之间吗? – MiKenning

+0

另外,谢谢!这个效果很好,我认为! – MiKenning

+0

是的,当然可以!添加到总分中,然后调用一个新的Intent。而且,你非常欢迎! :) – Swayam

相关问题