我在电台组创建了2组,其中一组问你是否擅长足球?另一个关于篮球的人。如果他们很好,我给了3分,我想计算我的计划的总分,但我不能。我需要帮助
private void addListenerOnButton() {
final RadioGroup football = (RadioGroup) findViewById(R.id.radioGroup1);
Button calc = (Button) findViewById(R.id.diabetriskbutton1);
final RadioGroup basketball = (RadioGroup) findViewById(R.id.radioGroup2);
calc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int IDfootball = football.getCheckedRadioButtonId();
RadioButton football = (RadioButton) findViewById(IDfootball);
if (IDfootball == R.id.item1yes) {
int scorefootball = 3;
} else if (IDfootball == R.id.item1no) {
int scorefootball = 2;
}
int IDbasketball = basketball.getCheckedRadioButtonId();
RadioButton basketball = (RadioButton) findViewById(IDbasketball);
if (IDbasketball == R.id.item1yes) {
int scorebasketball = 3;
} else if (IDbasketball == R.id.item1no) {
int scorebasketball = 2;
}
scoretotal = scorefootball + scorebasketball ;
}
});
}`
答案 0 :(得分:1)
尝试删除这些行。我认为你的编译器会将你的onClick方法中的RadioGroup football | basketball变量与相同的局部变量混淆。
我也认为它们是不必要的,因为它们在宣布之后从未使用过。 (您已经将从getCheckedRadioButtonId()获得的ID与R.id.itemYes | No
进行比较 RadioButton football = (RadioButton) findViewById(IDfootball);
RadioButton basketball = (RadioButton) findViewById(IDbasketball);
另一点。你在if和else块中声明了scorefootball和scorebasketball。编译器不会在各自的块之外看到这些变量。 这一行会引发错误:
scoretotal = scorefootball + scorebasketball;
请在类似之外声明:
int scorefootball = 2;//default value
int scorebasketball = 2;//default value
...
if (IDfootball == R.id.item1yes) {
scorefootball = 3;
} else if (IDfootball == R.id.item1no) {
scorefootball = 2;
}
...
if (IDbasketball == R.id.item1yes) {
scorebasketball = 3;
} else if (IDbasketball == R.id.item1no) {
scorebasketball = 2;
}
scoretotal = scorefootball + scorebasketball ;
还可以发贴你的xml吗?我想知道你为什么要比较从不同RadioGroups的getCheckedRadioButtonId获得的ID与相同的R.id.item1Yes | No