【发布时间】:2020-10-22 13:35:06
【问题描述】:
我已经研究和查找了几个小时的不同表单,但似乎无法找到将多个 switch 语句添加在一起的方法。我已经缩短了开关,以便于发布。
我教过我可以在最后一次切换后进行添加,但似乎无法正常工作,因为应用程序崩溃且没有错误。
TextView tvanwser;
int total,ttband1,ttband2,ttband3;
{
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case R.id.rbBlack1:
band1.setBackgroundColor(getResources().getColor(R.color.colorBlack));
ttband1 = 0;
break;
case R.id.rbBrown1:
band1.setBackgroundColor(getResources().getColor(R.color.colorBrown));
ttband1 = 1;
break;
case R.id.rbOrange1:
band1.setBackgroundColor(getResources().getColor(R.color.colorOrange));
ttband1 = 3;
break;
}
});
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case R.id.rbBlack2:
band2.setBackgroundColor(getResources().getColor(R.color.colorBlack));
ttband2 = 2;
break;
case R.id.rbBrown2:
band2.setBackgroundColor(getResources().getColor(R.color.colorBrown));
ttband2 = 3;
break;
case R.id.rbOrange2:
band2.setBackgroundColor(getResources().getColor(R.color.colorOrange));
ttband2 = 4;
break;
}
});
total = ttband1 + ttband2;
tvAnswer.setText(total);
}
【问题讨论】:
-
the app crashes with no error.这听起来不正确,你看过你的日志吗 -
我不认为你的 switch case 是导致你的应用崩溃的问题。
-
你需要通过“加在一起”来解释你的意思。
-
我有 3 个 RadioGroups,每个 RadioGroups 有 5 个单选按钮。因此,当您单击每个组中的 1 个按钮时,它会将它们加在一起并输出答案@HasinduDahanayake
标签: java android switch-statement