[关闭]
@zhongzilu 2016-05-07T08:02:02.000000Z 字数 1119 阅读 3595

如何在Android Studio/IntelliJ中创建Live Templates

Android Studio IntelliJ LiveTemplate


何为Live Templates?

frequently-used or custom code constructs that you can insert into your source code file quickly, efficiently and accurately.
快速,高效,准确的将常用或自定义构造代码插入到你的源代码文件中

通俗的讲,就是只需要输入几个简写的英文字母就可以快速自动插入一行或多行代码。

在Android Studio/IntelliJ编辑器中,默认使用Ctrl + J快捷键可以调出Live Templates,可以自动补全所设置的模板的代码,善用它能够很大程度上减少开发时间。

举个例子,输入psfi,然后按回车键,编辑器将会自动生成代码public static final int,这就节省了19个要输入的字母。

Android Studio/IntelliJ编辑器中预置了一些常用的Live Templates,大家可以自行去查看,查看位置File ==> Settings ==> Editor ==> Live Templates
Live Templates

如何创建自定义的Live Templates?

Alt text

举例

我用一个快速插入RecyclerView控件代码的例子来举例,设置如下图:
RecyclerView Live Template

填写相关内容完毕之后 ,注意下方的一行红字,此时定义的Live Template还没生效,需要点击Define,然后选择该Live Template要应用到什么样的场景中。因为这里我定义的是安卓的控件代码,所以就归类为XML
Define Live Template

点击OK按钮生效。注意勾选右下角的Reformat according to style选项

要想使用刚才定义的Live Template,只需要在android的XML布局文件中输入rcv,然后按回车键就可以了,如下图所示:
Use Live Template


以上就是如何在Android Studio/IntelliJ编辑器中创建Live Template的方法,相信富有创意的各位能够创建出更多的更方便的Live Template,这对于今后的代码编写大有裨益

另外,我在这里推荐一篇博客:http://blog.csdn.net/DesmondJ/article/details/47017205

原文地址:https://zybuluo.com/zhongzilu/note/369593
@author zhongzilu

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注