开发者

How to count yes and no answers using radio buttons

I don't know if this was asked or not (I searched a little but no result) but I'm kinda time pressed and I need help I've been developing an application in Android and I've only started under this platform for 3 months

I have a choice test with different questions and yes and no answers using radio buttons

I want to count the "yes" answers but even like this if I put r1.isChecked() instead of for and buttons[i].isChecked() it counts the clicks

Here is what I tried until now and I get force close everytime I click on the first radiobutton RadioButton[]buttons={rb1, rb3,rb5} ;

public void onClick(View view){



        checkStates(buttons);


}
private void  checkStates(RadioButton[] buttons) { 

          for (int i=0; i<buttons.length; i++) {
           开发者_StackOverflow社区  if (buttons[i].isChecked()) 
                da++;}
tv.setText("Result:"+yes);
    }

How can I tell my appplication that the radiobuttons are already checked and no need to increment it on a second click (on the same radio button?)

Is there a way to count using the ids or the name of the radiobuttons?

If so how should I do it? Some tutorials (I love them) and tips would help a lot.

Thanks.


You wouldn't want to count each click or even each "state change" because that wouldn't be accurate.

The best thing to do would be to create a function called checkStates(RadioButton[] buttons) { }

You would call this function each time the user clicks a RadioButton.

You would have all your RadioButton objects stored in an array, which you pass to the function, and then the function goes through the list of RadioButtons and checks their state. If it counts 6 as being Yes, then go to your other activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜