简体   繁体   English

案例陈述在外部开关

[英]Case statement is outside switch

I'm doing some experimenting trying to get a toggle button to move a image view depending on if the button is pressed or not. 我正在做一些实验,尝试获取一个切换按钮以根据是否按下按钮来移动图像视图。 currently it is stating that the case is outside of the switch and I'm not sure how to fix it. 目前,这表明该案件不在交换机之内,我不确定如何解决。

switch(v.getId())

{
    case R.id.moveButton: {


        boolean check = ((ToggleButton) v).isChecked();
        if (check) {
            ImageView img_animationOne = (ImageView) findViewById(R.id.player);

            TranslateAnimation animation = new TranslateAnimation(.0f, 200.0f,
                    0.0f, 0.0f);
            animation.setDuration(1000);
            animation.setRepeatCount(1);
            animation.setRepeatMode(2);
            animation.setFillAfter(true);
            img_animationOne.startAnimation(animation);

        } else {
            ImageView img_animationOne = (ImageView) findViewById(R.id.player);

            TranslateAnimation animation = new TranslateAnimation(200.0f, 0.0f,
                    0.0f, 0.0f);
            animation.setDuration(1000);
            animation.setRepeatCount(1);
            animation.setRepeatMode(2);
            animation.setFillAfter(true);
            img_animationOne.startAnimation(animation);

        }

    }
}

You forgot 你忘了

break

So you run all code after label R.id.moveButton every time. 因此,您每次都在标签R.id.moveButton之后运行所有代码。

That should be 那应该是

case R.id.moveButton:
    //your code
    break;

Change your code like this.This is the actual syntax for swtich case statements. 像这样更改代码。这是swtich case语句的实际语法。

switch(v.getId())

    {
        case R.id.moveButton: 


            boolean check = ((ToggleButton) v).isChecked();
            if (check) {
                ImageView img_animationOne = (ImageView) findViewById(R.id.player);

                TranslateAnimation animation = new TranslateAnimation(.0f, 200.0f,
                        0.0f, 0.0f);
                animation.setDuration(1000);
                animation.setRepeatCount(1);
                animation.setRepeatMode(2);
                animation.setFillAfter(true);
                img_animationOne.startAnimation(animation);

            } else {
                ImageView img_animationOne = (ImageView) findViewById(R.id.player);

                TranslateAnimation animation = new TranslateAnimation(200.0f, 0.0f,
                        0.0f, 0.0f);
                animation.setDuration(1000);
                animation.setRepeatCount(1);
                animation.setRepeatMode(2);
                animation.setFillAfter(true);
                img_animationOne.startAnimation(animation);

            }
    break;

        }

If the above did not work, convert the switch statement into an if-else statement. 如果上述方法不起作用,请将switch语句转换为if-else语句。 In a regular Android project, constants in the resource R class are declared like this: 在常规的Android项目中,资源R类中的常量声明如下:

public static final int main=0x7f030004;

However, as of ADT 14, in a library project, they will be declared like this: 但是,从ADT 14开始,在图书馆项目中,它们将这样声明:

public static int main=0x7f030004;

In other words, the constants are not final in a library project. 换句话说,常数在库项目中不是最终的。 Therefore your code would no longer compile. 因此,您的代码将不再编译。 To convert into if statement, In Android Studio Move your cursor to the switch keyword and press Alt + Enter then select 要将其转换为if语句,请在Android Studio中将光标移至switch关键字,然后按Alt + Enter然后选择

Replace 'switch' with 'if' 将'switch'替换为'if'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM