有没有一种方法可以将2个或更多的switch语句结合在一起?

huangapple go评论77阅读模式
英文:

Is there a way to add 2 or more switch statements together

问题

我已经进行了几个小时的研究,查找不同的形式,但似乎找不到将多个switch语句组合在一起的方法。我已经缩短了switch语句,以便更容易发布。

我认为我可以在最后一个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(String.valueOf(total));
}
英文:

I have been researching and looking up different forms for hours now and cant seem to find a way to add multiple switch statements together. I have shortened the switch's to make it easier to post.

I taught I could just do the adding after the last switch but doesn't seem to work as the app crashes with no error.

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

答案1

得分: 1

我不太清楚您想要实现什么,也不认为您的崩溃与您的代码有关。但我可以告诉您,您的这行代码:

total = ttband1 + ttband2;

不会按照您的意图工作。
在元素上添加侦听器(setOnCheckedChangeListener)将注册一个函数,该函数将在侦听器的条件达到时执行。在这种情况下:一旦单选按钮组被选中或取消选中。

这意味着只有在与UI元素交互时,ttband1 和 ttband2 的值才会发生变化。

total = ttband1 + ttband2;
tvAnswer.setText(total);

然而,您的最后两行代码将会在侦听器添加后立即执行。此时,ttband1 和 ttband2 的值尚未修改,可能为 0。

为了在每次 ttband1 或 ttband2 的值发生变化时保持更新,您需要将这些行移到您的侦听器函数中。

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

       total = ttband1 + ttband2;
       tvAnswer.setText(total);
      }
});
英文:

I don't really know what you are trying to achieve and don't think that your crashes are related to your code. But i can tell that your line

total = ttband1 + ttband2;

will not work as you intend.
Adding a listener (setOnCheckedChangeListener) on an element will register a function that executes as soon as the condition for the listener is reached. In this case: as soon as the radiogroup is checked or unchecked.

This means that the values for ttband1 and ttband2 are only changed while you interact with the UI elements.

total = ttband1 + ttband2;
tvAnswer.setText(total);

Your last two lines of code however will execute immediately after your listeners are added. At this point your values ttband1 and ttband2 were not modified and are probably 0.

To stay up-to-date everytime your values ttband1 or ttband2 are changed you need to move the lines into your listener functions.

 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;
}
total = ttband1 + ttband2;
tvAnswer.setText(total);
});

答案2

得分: 1

I never converted the int to a string which caused the app to crash

total = total1 + total2 + total3;
tvanwser.setText(String.valueOf(total));
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;
       }

            total1 = ttband1;
            total = total1 + total2 + total3;
            tvanwser.setText(String.valueOf(total));
      });
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));
                ttband2 = 0;
                break;
            case R.id.rbBrown1:
                band1.setBackgroundColor(getResources().getColor(R.color.colorBrown));
                ttband2 = 1;
                break;
            case R.id.rbOrange1:
                band1.setBackgroundColor(getResources().getColor(R.color.colorOrange));
                ttband2 = 3;
                break;
       }

            total2 = ttband2;
            total = total1 + total2 + total3;
            tvanwser.setText(String.valueOf(total));
      });
英文:

I never converted the int to a string witch caused the app to crash

   total = total1 + total2 + total3;
tvanwser.setText(String.valueOf(total));
 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;
}
total1 = ttband1;
total = total1 + total2 + total3;
tvanwser.setText(String.valueOf(total));
});
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));
ttband2 = 0;
break;
case R.id.rbBrown1:
band1.setBackgroundColor(getResources().getColor(R.color.colorBrown));
ttband2 = 1;
break;
case R.id.rbOrange1:
band1.setBackgroundColor(getResources().getColor(R.color.colorOrange));
ttband2 = 3;
break;
}
total2 = ttband2;
total = total1 + total2 + total3;
tvanwser.setText(String.valueOf(total));
});

huangapple
  • 本文由 发表于 2020年10月22日 21:35:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64483421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定