案例陈述是外部开关

时间:2017-04-18 13:03:30

标签: java android

我正在做一些尝试尝试获取切换按钮来移动图像视图,具体取决于是否按下了按钮。目前它说的是外壳在开关之外,我不知道如何解决它。

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

        }

    }
}

2 个答案:

答案 0 :(得分:2)

你忘记了

break

所以每次都在标签R.id.moveButton之后运行所有代码。

那应该是

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

答案 1 :(得分:0)

像这样更改你的代码。这是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;

        }

如果上述操作无效,请将switch语句转换为if-else语句。 在常规的Android项目中,资源R类中的常量声明如下:

public static final int main=0x7f030004;

但是,从ADT 14开始,在库项目中,它们将被声明为:

public static int main=0x7f030004;

换句话说,常量在库项目中不是最终的。因此,您的代码将不再编译。 要转换成if语句, 在Android Studio中 将光标移至切换关键字,然后按Alt + Enter,然后选择

  

将'switch'替换为'if'

相关问题