如何在android studio中移动(上/下)代码行?

58 ide intellij-idea android-studio

在eclipse中,我们使用Alt+ / 来向上或向下移动线.

android studio中有任何快捷方式吗?或者任何快速避免复制和粘贴的方法?

Ing*_*ers 110

添加,删除和移动线

要移动一条线:

将插入符号放在要移动的行上.

执行以下操作之一:

在主菜单上,选择Code | Move Line Up or Code | Move Line Down.

Shift+ Alt+ UpShift+ Alt+ Down.

  • [此](/sf/answers/2569904081/) 在 Ubuntu 18.04 中适用于我。 (3认同)

Mil*_*nia 7

如果你想要eclipse的确切行为,你可以这样做:

文件 - >设置 - >键盘映射 - >代码 - >折叠 - >分配Alt+ / 到" 向上/向下移动 "而不是" 向上/向下移动语句 "


Bow*_*owi 5

Android Studio 中有(至少)两种向上/向下移动线:“智能”一种和“哑”一种。就像 IngoAlbers 说的那样,愚蠢的人 ( Shift+ Alt+<Arrow>)只是移动的线。

使用Ctrl+ Shift+<Arrow>代替,使得功能更加智能化:

  • 它不会离开当前的“上下文”:

    public static void test() {
        int i = 5;
        Runnable theodor = new Runnable() {
            public void run() {
                System.out.println("Hi!");
            }
        };
    }
    
    Run Code Online (Sandbox Code Playgroud)

    int i = 5;线向下移动一步,为您带来:

    public static void test() {
        Runnable theodor = new Runnable() {
            public void run() {
                System.out.println("Hi!");
            }
        };
        int i = 5;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 它将方法保持在一起:

    public static void hello() {
        System.out.println("Hello!");
    }
    
    public static void dinner() {
        System.out.println("Dinner's ready!");
    }
    
    public static void sleep() {
        System.out.println("Good night.");
    }
    
    Run Code Online (Sandbox Code Playgroud)

    将线public static void sleep() {向上移动一步移动sleep()上面的完整方法dinner()

    public static void hello() {
        System.out.println("Hello!");
    }
    
    public static void sleep() {
        System.out.println("Good night.");
    }
    
    public static void dinner() {
        System.out.println("Dinner's ready!");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在大多数情况下,这只是令人讨厌。;-)