OS X下使用Android Studio快捷键提升效率
原文:http://blog.stablekernel.com/level-up-with-android-studio-shortcuts-and-live-templates/
利用 Android Studio 键盘快捷键、重构选项和活动模板快速导航和重构你的代码。
快捷键的配置需要仔细,避免冲突,这样才可以把快捷键的各个动作连接起来,提高效率。
_注:这里讨论的所有快捷键都是 __Android Studio for Mac OS X __的默认快捷键。推荐禁用覆盖 __F# __键的 __Mac _快捷键,这能使得很多以下的快捷键更容易操作。

简易快捷键
我们的目标是提高速度和准确性。提速的第一步是停止使用鼠标。第一组快捷键是基于你很有可能经常做的简易操作设置的,如果你用鼠标做了这些操作,你就多花了10倍的时间。
- 编译java源码:SHF+CMD+F9
- 建立工程:CMD+F9
- 运行当前配置:CTR+R
- 在调试器里运行:CTR+D
- 打开工程属性:CMD+;
- 打开Android Studio参数:CMD+,
- 查找任意命令:SHF+CMD+A
- 自动格式代码:OPT+CMD+L
- 删除行:CMD+DELETE
- 复制行/选中区域:CMD+D
- 复制行:CMD+C (未选中任意区域时)
- 选择下一个:CTR+G
基于范围的选择区域
另一个重要的避免接触鼠标和加速你的工作流程的方法,是改进对文本的选择。当你选择文本的大部分时候,你是在试图将它选择为一个有效的单元。当很多代码是嵌套的时候,尝试找出合适的插入语并选择这些代码是很难的一件事。快捷键把这件事变得简单快捷了。
- 选择下一个更高的范围:OPT+UP
- 选择下一个更低的范围:OPT+DOWN

简单的重构
我们的下一个目标是在我们想要做简单重构的地方识别出一些现存的代码,而且要做得又快又准确,不会让心跳漏拍。在我们想要改变的范围内,你只需敲一下键盘,就能做到下列这些事:
- 重命名:SHF+F6
- 提取域:OPT+CMD+F
- 提取变量:OPT+CMD+V
- 提取参数:OPT+CMD+P
- 提取常量:OPT+CMD+C
- 提取方法:OPT+CMD+M

为代码作导航
我们把大量的时间花在了在修改之前理解现有的代码上。如果我们能在代码的不同区域之间无冲突地切换,那么我们就能保持上下文的完整性,同时保证速度。
- 打开类:CMD+O
- 打开文件:SHF+CMD+O
- 找到标记:OPT+CMD+O
- 查看标记完成:CMD+B
- 查看完成(如果是接口或者抽象):OPT+CMD+B
- 找出符号所有的引用处:OPT+F7
- 导航回上个位置:CMD+[
- 导航到前个位置:CMD+]
- 切换到最近用过的文件:CRT+TAB
代码生成
不幸地是,我们耗费大量的时间,一遍又一遍地写着同样类型的代码。我们试着保持DRY原则,但是到最后,大多数情境下,重复的代码是不可避免的。为了应对这些情况,Android Studio 提供了一些不同的方法作为帮助。
意图操作(又称读心操作)
如果代码还不是完成品,当你认识到 OPT+RETURN 的威力时,它将成为你最好的朋友。它基本上能读取你的想法,并对你想要对光标位置所在地的上下文做的改动给出建议,尤其是对那些标注为红色的、有下划线的或者高亮的文本。
**If/Switch actions: **OPT+RETURN

还有,如果你习惯在定义某些函数或变量之类的东西前就引用它们,Android Studio只需敲一下键盘,就可以拥有足够的信息来创建它们的存根。这对你能想到的任何对象都适用,比如类、公开方法、私有方法、域、变量、参数等等。在生成代码时,它将和类型智能匹配。
创建方法:(众多例子中的一例)

和上下文相关的操作太多了,我甚至都不知道从哪个说起。我建议,在你的意图可以被推断的大量地方放上你的光标,然后按下 OPT+RETURN,看看你能做些什么。
自动完成的覆盖方法
无论啥时候,当你在一个拥有可以被覆盖的方法的子类里,你只需要键入该方法名,把它从智能弹出菜单里选中,为它自动生成一个方法存根即可。

活动模板
循环
- 单次循环:“[变量名].for”+TAB
- 第 i 次循环:“[变量名].fori”+TAB
- 第 r 次循环(可保留的):“[变量名].forr”+TAB

日志
- 生成调试日志:“logd”+TAB
- 生成错误日志:“loge”+TAB
- 生成信息日志:“logi”+TAB
- 生成TAG声明:“****logt”+TAB
- 生成参数日志:“logm”+TAB
- 生成方法回归日志:“logr”+TAB

等等等等****……
- 创建带参数的newInstance() 方法:“newInstance”+TAB
- 创建Toast声明;“Toast”+TAB
- 增加布局高度=“包内容”到xml控件:“lhw”+TAB
我可以一直列举下去,几天几夜都行。我会鼓励你打开Android Studio的参数面板,探索活动模板,看看所有可能会发生的事情。没找到你要的选项?你也可以随心所欲地创建属于你自己的活动模板。
拥有你自己的工具
本文的寓意是这样的:如果你没有好好利用手头所有可用的工具,那么你就在思考和完成品之间竖起一道障碍。移除这些障碍不仅能让你变得更快,还能帮助你更好地在代码中表述你自己。像在任何你依赖工具来进步的职业里一样,对工具足够了解,才能拓展你的能力,把想法转化为结果。
像任何依赖工具的职业一样,需要经过练习来掌握工具。一开始会很难,但是要强迫你自己不要用鼠标操作,不要用困难的方法去写代码,而是使用这些快捷键。你用得越多,操作就越简单易用,很快你就能超越你之前的工作效率。
原文:
Simple Shortcuts
Our goals here are to increase speed and accuracy. The first step to achieving speed is to stop touching the mouse. This first group of shortcuts is based around simple operations that you probably do a lot, but if you do it with the mouse, you’re spending 10x longer getting there.
Compile java sources: SHF+CMD+F9
Build the project: CMD+F9
Run the current configuration: CTR+R
Run in debugger: CTR+D
Open project properties: CMD+;
Open Android Studio preferences: CMD+,
Find any command: SHF+CMD+A
Auto-format code: OPT+CMD+L
Delete line: CMD+DELETE
Duplicate line/selection: CMD+D
Copy line: CMD+C (with nothing selected)
Select next occurance(s): CTR+G
Scope based selection
Another important way to avoid having to touch the mouse and speed up your workflow is improving text selection. A large portion of the time when you are selecting text, you are trying to select it as a valid unit. Trying find the right parenthesis to select when many things are nested is difficult. This shortcut makes it easier and faster.
Select next higher scope: OPT+UP
Select next lower scope: OPT+DOWN
Simple Refactorings
Our next goal is to identify some existing code where we want to perform simple refactorings, and do it quickly and accurately without skipping a beat. With the cursor in the scope of the change we want to make you can perform the following with a single keystroke:
Rename: SHF+F6
**Extract Field: OPT+CMD+F
Extract Variable: **OPT+CMD+V
**Extract Parameter: **OPT+CMD+P
**Extract Constant: **OPT+CMD+C
**Extract Method: **OPT+CMD+M
Navigating the code
A large amount of our time is spent trying to understand the existing code base before making a change. If we can make navigating in and out of different areas of the code base frictionless, then we can maintain context and speed.
**Open class: CMD+O
Open file: SHF+CMD+O
Find symbol: OPT+CMD+O
View implementation of symbol: **CMD+B
View implementation (if interface or abstract): OPT+CMD+B
Find all usages of a symbol: OPT+F7
Navigate back to last position: CMD+[
Navigate forward to previous position: CMD+]
Switch to recently used files: CRT+TAB
Code Generation
Unfortunately we spend a lot of time writing the same type of code over and over again. We try to keep things DRY, but at the end of the day, repeating code is unavoidable in a lot of situations. For these, assistance is provided in a few different ways:
Intention Actions (aka Mindreading Actions)
If it’s not already the case, OPT+RETURN will become your best friend once you realize its power. It basically reads your mind and suggests things you probably want to do in the context of where the cursor position is. Especially of it’s on red, underlined or highlighted text.
**If/Switch actions: **OPT+RETURN
And if you get in the habit of writing the code that makes use of something before it actually exists, Android Studio will have enough information to create the stubs for it with just a keystroke. This goes for almost anything you can think of. Classes, public methods, private methods, fields, variables, parameters and so on. And it will be smart about the types being used when generating code.
**Create method: **(one of many examples)
There are so many of these context-aware actions that I can’t even begin to scratch the surface here. I recommend that you put the cursor in lots of different places where your intentions could be inferred and then press OPT+RETURN to see what you can do.
Auto-complete override methods
Anytime you are in a subclass that has methods available to override, you can just start typing the name of the method and select from the intellisense popup to auto generate a method stub for it.
Live Templates
Loops
For each loop: “[variable name].for”+TAB
For i loop: “[variable name].fori”+TAB
For i loop (reversed): “[variable name].forr”+TAB
Logs
Generate debug log statement: “logd”+TAB
Generate error log statement: “loge”+TAB
Generate info log statement: “logi”+TAB
Generate TAG declaration: “logt”+TAB
Generate parameter logging: “logm”+TAB
**Generate method return log: **”logr”+TAB
On and on and on…
Creating newInstance() method with arguments: “newInstance”+TAB
Create a Toast statement: “Toast”+TAB
Add layout_height=”wrap_content” to xml widget: “lhw”+TAB
…
I could go on for days here. I encourage you to open the Android Studio Preferences and explore the Live Templates section to see everything that is possible. Don’t see one you want? Fear not, you can create your own as well!
Own your tools
The moral of the story is: if you aren’t taking advantage of all that your tools make available to you, then you are leaving a barrier between thought and implementation. Removing barriers not only make you faster, but better able to express yourself in the code. Like any profession where you rely on tools to excel (musicians, carpenters, surgeons, etc), it’s about knowing the tools so well that they become an extension of you, enabling a direct path from thought to results.
Like any profession that relies on tools, it also takes practice. It’ll be hard at first, but force yourself to stop touching the mouse and stop typing things out the hard way, and use these shortcuts. The more you do, the easier it gets, and you’ll quickly blow away your previous levels of productivity.






