2012-09-03 2 views
0

나는 각각의 대답이 총 점수에 특정 수의 점수를 추가하는 퀴즈를 설정하려고합니다. 지금은 라디오 버튼에 관심이 있습니다. radiogroup을 설정했으며 총계에 추가 할 라디오 버튼을 선택했습니다. 이것은 단지 내 프로그램의 한 부분입니다.퀴즈 앱에서 점수 시스템을 어떻게 설정합니까?

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); 


    } 

} 

그것은 결과 클래스를 확장하고 나는이 그래서 점수의 정수를 상속했다 :

다음은 실제 XML 레이아웃 파일의 클래스 파일입니다. 여기에 점수 클래스는 다음과 같습니다

public class Results extends Activity { 

    private int score; 

    public int getScore() { 
     return score; 
    } 

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

} 

나는 인터넷 검색에서 해당 구조를 가지고 및 유효성 나는 확신하지만 난 기본적인 아이디어가 생각 의문이다. 누구든지 나를 도울 수 있습니까?

답변

1

개별 RadioButton을 선언하는 대신 RadioGroup을 선언하십시오.

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

물론 가능합니다! 총 점수에 추가 한 다음 새로운 인 텐트를 호출하십시오. 그리고, 당신은 가장 환영합니다! :) – Swayam

관련 문제