studio 快捷键

注释代码(反注释代码):

  • Ctrl+L

代码格式化

  • Win/Linux下: Ctrl + Alt + L
  • Mac: Option + Command + L

导入相应的包申明 import :

  • Alt+Enter  (注意:在xml中是提取资源,如从布局文件中提取字符串到strings.xml文件中
 

定位代码修改处

 

(Navigate | Last Edit Location) brings you back to the last place where you made changes in the code.

 

 

Pressing Ctrl+Shift+Backspace a few times moves you deeper into your changes history.

 

 

  • Ctrl+Shift+Backspace

 

查看最近常编辑的文件:

  • Ctrl+E

 

 

跳转到语法错误

Use F2/Shift+F2 keys to jump between highlighted syntax errors.

 

  • F2/Shift+F2

 

Use Ctrl+Alt+向上箭头/Ctrl+Alt+向下箭头 shortcuts to jump between compiler error messages or search operation results.

To skip warnings right click on the validation side bar / marker bar and choose Go to high priority problems only.

从剪切板中粘贴最近剪切复制的内容:

 

  • Ctrl+Shift+V

 

 

生成方法注释

先输入 “/**” ,然后回车,就会自动加上 Parameters关键字。

 

 

获取帮助:

快捷键:ctrl+shift+A  ,输入关键字。
或者是 Help——find action

 

 

编辑快捷键

捷键 解释
Ctrl + Space Basic code completion (the name of any class,method or variable)
Ctrl + Shift + Space Smart code completion (filters the list of methods and variables by expected type)
Ctrl + Shift + Enter Complete statement
Ctrl + P Parameter info (within method call arguments)
Ctrl + Q Quick documentation lookup
Shift + F1 External Doc
Ctrl + mouse over code Brief Info
Ctrl + F1 Show descriptions of error or warning at caret
Alt + Insert Generate code… (Getters, Setters, Constructors, hashCode/equals, toString)
Ctrl + O Override methods
Ctrl + I Implement methods
Ctrl + Alt + T Surround with… (if..else, try..catch, for, synchronized, etc.)
Ctrl + / comment注释/反注释一行代码comment
Ctrl + Shift + / 注释/反注释代码段comment
Ctrl + W Select successively increasing code blocks
Ctrl + Shift + W Decrease current selection to previous state
Alt + Q Context info
Alt + Enter Show intention actions and quick-fixes
Ctrl + Alt + L Reformat code格式化代码
Ctrl + Alt + O Optimize imports导入
Ctrl + Alt + I Auto-indent line(s)
Tab / Shift + Tab Indent/unindent selected lines
Ctrl + X or Shift + Delete Cut current line or selected block to clipboard
Ctrl + C or Ctrl + Insert Copy current line or selected block to clipboard
Ctrl + V or Shift + Insert Paste from clipboard
Ctrl + Shift + V Paste from recent buffers…
Ctrl + D Duplicate current line or selected block
Ctrl + Y Delete line at caret
Ctrl + Shift + J Smart line join
Ctrl + Enter Smart line split
Shift + Enter Start new line
Ctrl + Shift + U Toggle case for word at caret or selected block
Ctrl + Shift + ] / [ Select till code block end/start
Ctrl + Delete Delete to word end
Ctrl + Backspace Delete to word start
Ctrl + NumPad+/- 折叠/展开代码块Expand/collapse code block
Ctrl + Shift + NumPad+ 展开所有代码块Expand all
Ctrl + Shift + NumPad- 折叠所有代码块Collapse all
Ctrl + F4 Close active editor tab
Ctrl + Shift + C Copy Path
Ctrl+ Alt + Shift + C Copy Reference

Ctrl + Period(句号) 将指针放入方法体中,代码折叠或展开
   
   
   
   

打开粘贴复制面板:

ctrl+shift+v
You can also change the size of the extended clipboard stack by navigating to File ➤
Settings ➤ Limits ➤ Editor ➤ Maximum Number of Contents to Keep in Clipboard.
You  can also compare any currently selected text with that of the most recent element in the 
extended clipboard by right-clicking the selection and selecting the Compare with Clipboard
menu item.

search and replace

快捷键 解释
Double Shift Search everywhere
Ctrl + F 查找Find
F3 Find next
Shift + F3 Find previous
Ctrl + R 替换Replace
Ctrl + Shift + F 查找Find in path
Ctrl + Shift + R Replace in path
Ctrl + Shift + S Search structurally (Ultimate Edition only)
Ctrl + Shift + M Replace structurally (Ultimate Edition only) 

 

 

Usage search

 

快捷键 解释
Alt + F7 / Ctrl + F7 Find usages / Find usages in file
Ctrl + Shift + F7 Highlight usages in file
Ctrl + Alt + F7 Show usages

 

 

 

Compile and Run

 

快捷键 解释
Ctrl + F9 Make project (compile modifed and dependent)
Ctrl + Shift + F9 Compile selected file, package or module
Alt + Shift + F10 Select configuration and run
Alt + Shift + F9 Select configuration and debug
Shift + F10 Run
Shift + F9 Debug
Ctrl + Shift + F10 Run context configuration from editor 

 

 

Debugging

 

快捷键 解释
F8 Step over
F7 Step into
Shift + F7 Smart step into
Shift + F8 Step out
Alt + F9 Run to cursor
Alt + F8 Evaluate expression
F9 Resume program
Ctrl + F8 Toggle breakpoint
Ctrl + Shift + F8 View breakpoints 

 

 

Navigation

 

快捷键 解释
Ctrl + N 跳转到制定的类中,包括内部类
Ctrl + Shift + N Go to file
Ctrl + Alt + Shift + N Go to symbol
Alt + Right/Left Go to next/previous editor tab
F12 Go back to previous tool window
Esc Go to editor (from tool window)
Shift + Esc Hide active or last active window
Ctrl + Shift + F4 Close active run/messages/find/… tab
Ctrl + G 跳转到指定行
Ctrl + E 打开最近编辑的文件界面
Ctrl + Alt + Left/Right Navigate back/forward
Ctrl + Shift + Backspace Navigate to last edit location
Alt + F1 Select current file or symbol in any view
Ctrl + B or Ctrl + Click 跳转到变量、方法、类的声明的地方
Ctrl + Alt + B Go to implementation(s)
Ctrl + Shift + I Open quick definition lookup
Ctrl + Shift + B Go to type declaration
Ctrl + U Go to super-method/super-class
Alt + Up/Down Go to previous/next method
Ctrl + ] / [ Move to code block end/start
Ctrl + F12 File structure popup
Ctrl + H 类的层次Type hierarchy
Ctrl + Shift + H Method hierarchy
Ctrl + Alt + H Call hierarchy
F2 / Shift + F2 Next/previous highlighted error
F4 / Ctrl + Enter Edit source / View source
Alt + Home Show navigation bar
Ctrl + F11 Toggle bookmark with mnemonic
Ctrl + #[0-9] Go to numbered bookmark
Shift + F11 Show bookmarks 

move移动代码的快捷键:

 

实例:快速跳转到某个class文件的指定行

方法:先按Ctrl + Shift + N ,然后输入你要打开的类的名字(不一定要完整输完),直到匹配唯一,然后在后面加上 “ :行数”

Refactoring

快捷键 解释
F5 Copy
F6 Move
Alt + Delete Safe Delete
Shift + F6 Rename
Ctrl + F6 Change Signature
Ctrl + Alt + N Inline
Ctrl + Alt + M Extract Method
Ctrl + Alt + V Extract Variable
Ctrl + Alt + F Extract Field  ,提取变量,以改变其初始化的位置
Ctrl + Alt + C Extract Constant
Ctrl + Alt + P Extract Parameter 
Alt + Enter 在xml中是提取资源,如从布局文件中提取字符串到strings.xml文件中
 

Refactoring Code

快捷键: Ctrl+Alt+Shift+T | Ctrl+T

 

Change Signature

修改方法的签名,如可见性,返回值等 快捷键: ctrl+F6

Type Migration

As the name suggests, type migration allows you to migrate from one Java type to another.
Let’s assume that you create a Person class. Further along in your development, you discover
that Person is too generic, so you create a Manager class that extends Person. If you want to
migrate all instances of Person to Manager, you can do this easily with type migration.Place your cursor on the String declaration (highlighted in bold in the following code
snippet) of the greetings method and press Ctrl+Shift+F6 | Cmd+Shift+F6 or choose
Refactor ➤ Type Migration. The resulting dialog box resembles that seen in Figure 4-4.
public String greetings(String greet){
    return "Hello " + greet;
} 

Move 移动文件

Refactor——move 或者是 F6 到指定的包中

Copy 文件

与move文件类似, Refactor——copy 或者是 F5
Safe Delete删除文件
delete键 或者   Refactor——delete 

Extract Variable

根据构造函数,自动生成变量名声明,快捷键为:Ctrl+Alt+V
51-extractvariable.gif

Extract Constant

快捷键: Ctrl+Alt+C
As you develop apps in Android, you will find yourself using a lot of Strings as keys—for
example, in Maps and Bundles. Therefore, extracting constants will save you a lot of time.
Define a method like the one seen in the following code snippet. Place your cursor anywhere  on the name_key string and press Ctrl+Alt+C | Cmd+Alt+C. The resulting dialog box should  look like Figure 4-9. Here, Android Studio provides a few suggestions for names. By  convention, constants in Java are all caps. Select NAME_KEY and press Enter.
private void addName(String name, Bundle bundle ){
    bundle.putString("name_key", name);
}
You should end up with a constant called NAME_KEY, which should be defined like this:
public static final String NAME_KEY = "name_key";

Extract Field

自动指定声明变量和初始化的位置,快捷键: Ctrl+Alt+F
如:现定义如下方法:
private Date getDate(){
return new Date();
}
然后按 Ctrl+Alt+F ,就可以指定初始化和声明的位置:

Extract Parameter

根据代码,生成方法的参数 ,快捷键: Ctrl+Alt+P
Extract Parameter allows you to extract a variable and place it as a parameter of the  enclosing method. Define a method in your Sandbox class:
private void setDate(){
         mDate = new Date();     
}
Place your cursor anywhere on Date() (highlighted in bold), press Ctrl+Alt+P | Cmd+Alt+P,
and press Enter. The resulting method should look like the following code snippet:
private void setDate(Date date){
        mDate = date;         
}
52-extractparam.gif

Extract Method

将某些代码块放到一个新的方法中 ,快捷键: Ctrl+Alt+M
53-extractmethod.gif

Push Members Down and Pull Members Up

将子类的变量放到父类中,快捷键:ctrl+alt+shift+T ,选择Push Members Down/Pull Members Up 。

When we talk about pulling something up, we mean that we will take something in the current class (usually a method or a field) and send it to the parent class or interface.

If the parent is a class, the content will be moved. If the parent is an interface, it will declare the method as part of the interface, keep the method in your class and add the @override annotation.

When we talk about pushing members down, that is exactly the opposite operation, we are sending content from the parent class/interface in the child class.

Replace Inheritance with Delegation

当想修改继承关系的时候,将继承替换为 父类为当前类的一个filed属性 。
Right-click (Ctrl-click on Mac) the com.apress.gerber.helloword package and choose
New ➤ Java Class. Name your class Patio and make it extend Sandbox:
public class Patio extends Sandbox {
     
}
Upon further analysis, we decide that Patio is not a Sandbox, but rather has a Sandbox. To
change this relationship, navigate to Refactor ➤ Replace Inheritance with Delegation. In the
resulting dialog box, click the Generate Getter for Delegated Component check box, shown
in Figure 4-14.
Your Patio class should now have a Sandbox member, as shown in the following code snippet:
public class Patio {
 
    private final Sandbox mSandbox = new Sandbox();
 
    public Sandbox getSandbox() {
        return mSandbox;
    }
}

Wrap Method Return Value

Wrapping a return value may be useful when you need to return an object rather than a  primitive (though there are other scenarios where you might want to wrap a return value). 
Place your cursor on the getChildren() method and navigate to Refactor ➤ Wrap Method  Return Value. Select the Use Existing Class check box and type java.lang.Integer as the  Name and value as the Wrapper Field, as shown in Figure 4-16. Now click Refactor and  notice that your getChildren() method returns an Integer object rather than a primitive int.

Replace Constructor with Factory Method

快捷键:   Alt+Insert
Place your cursor inside the enclosing brackets of the Sandbox class definition. Press  Alt+Insert | Cmd+N and select Constructor to generate a new constructor. Choose both  members, shown in Figure 4-17, and click OK.
Place your cursor anywhere in the newly defined constructor, shown in the following code
snippet, and then navigate to Refactor ➤ Replace Constructor with Factory Method. The
resulting dialog box looks like Figure 4-18. Click Refactor to generate a factory method.
public Sandbox(String shovel, int children) {
    mShovel = shovel;
    mChildren = children;
}
Notice that the constructor is now private and that a new static method returns an instance
of the Sandbox class, as shown in the following code snippet. This operation is particularly
useful if you are creating a singleton.
public static Sandbox createSandbox(String shovel, int children) {
     return new Sandbox(shovel, children);
}

Convert Anonymous to Inner

将匿名类转换为内部类:快捷键:Alt+Enter
In the constructor of your Sandbox class, add the following line:
new Thread(new Runnable()).start();  
Place your cursor on Runnable() and press Alt+Enter to invoke the code-completion
operation. Then select Implement Methods. Select the run method and click OK. Your code
should look something like the following code snippet:
new Thread(new Runnable() {
       @Override
       public void run() {
           //do something
       }
   }).start();
 Place your cursor on Runnable() and navigate to Refactor ➤ Convert Anonymous to Inner.
Android Studio suggests MyRunnable as a class name for you, as shown in Figure 4-19.
Deselect the Make Class Static check box and click OK. Notice that you now have a private
inner class called MyRunnable in Sandbox.java that implements the Runnable interface. This
example doesn’t do much; however, you may have opportunities to use this operation when
delegating the behaviors of Views.

VCS/Local History

快捷键 解释
Ctrl + K Commit project to VCS
Ctrl + T Update project from VCS
Alt + Shift + C View recent changes
Alt + BackQuote (`) ‘VCS’ quick popup
 

Live Templates

快捷键 解释
Ctrl + Alt + J Surround with Live Template
Ctrl + J Insert Live Template
iter Iteration according to Java SDK 1.5 style
inst Check object type with instanceof and downcast it
itco Iterate elements of java.util.Collection
itit Iterate elements of java.util.Iterator
itli Iterate elements of java.util.List
psf public static final
thr throw new 
 
实例:自动完成 for 语句
方法:按 Ctrl + J ,然后选择"fori"
 
更多代码模版,见File——settings——Editor——Live Templates
 

General

快捷键 解释
Alt + #[0-9] Open corresponding tool window
Ctrl + S Save all
Ctrl + Alt + Y Synchronize
Ctrl + Shift + F12 Toggle maximizing editor
Alt + Shift + F Add to Favorites
Alt + Shift + I Inspect current file with current profile
Ctrl + BackQuote (`) Quick switch current scheme
Ctrl + Alt + S Open Settings dialog
Ctrl + Alt + Shift + S Open Project Structure dialog
Ctrl + Shift + A Find Action
Ctrl + Tab Switch between tabs and tool window 

界面快捷键

Using Code Generation

先进行优化设置:
 Click File ➤ Settings ➤ Code Style ➤ Java ➤ Code
Generation to bring up the Settings dialog box with the Code Generation tab selected. If the
Field and Static Field text boxes do not contain m and s respectively, type them there now
and click Apply and then OK, as shown in Figure 3-8.
这样当设置属性field(变量)的时候,使用类似规则:
public String mName;

 Code Generation  之  Constructors构造器

快捷键Alt+insert ,然后选择constructors ,默认的构造器是无参数的。如果想构造有参数的构造器,前提是你的类中有非final变量,然后当你用alt+insert产生构造器的时候,就有有个面板让你选择用哪些变量作为构造器参数:

 Code Generation 之 Getters/Setters

Alt+insert,选择 getter或者setter 。因为前面设置属性以m开头,所以当使用generation的getter,setter的时候,得到的方法名中不包含m前缀,如下图:

Code Generation 之 Override Methods

Alt+insert,选择 Override Methods .

Code Generation 之 Delegate Methods

Alt+insert,选择Delegate Methods。 说白了就是调用父类的方法,当然你可以再自己修改下。
字面上翻译就是“生成代理方法”,就是如果你有一个类的属性,只要它不是基本类型(int, long, float...),就根据这个类型的所有父类型(包括接口)生成同名的方法,并调用父类型的方法。
这么解释起来有点抽象,你可以创建一个属性,比如
public class Test {
private String testStr; //在这个变量名上点右键“generate delegate methods"
...
你会发现所有String类以及父类Object类的所有方法都列出来了,你可以生成其中某个或全部方法的代理方法,例如生成length()方法的代理方法如下:
public int length() {
return testStr.length();
}
你可以看到”代理方法“一般只是简单的对父类型的同名方法做了调用,一般你还需要添加一部分功能,但是eclipse已经把重复劳动的那一部分替你做了。

 

Inserting Live Templates

  快速插入代码块:File——settings——Editor——Live Templates

快捷键为:
有时候也可以在Editor中,输入类似 psf之类的,就会出现代码块提示

Styling Your Code

File——settings——Editor——Code Style ——java

Surrounding With

将代码用if-else 或者try catch包围:
快捷键:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值