前言
安卓开发者现如今主流的编译器就是 Android Studio (以下简称AS),而 AS 是基于 IDEA 而定制化开发的编译器。AS 为我们提供了大量能够减少编码量和编码效率的功能,本文着重讲解 AS 自带的 Live Templates 和自定义 Live Templates。
正文
AS 自带的 Live Templates
如何查看 AS 的 Live Templates呢? 看图:
![img_24bb195d941e0b5f0a695baefe0e1e30.png](https://yqfile.alicdn.com/img_24bb195d941e0b5f0a695baefe0e1e30.png?x-oss-process=image/resize,w_1400/format,webp)
从图中可以看到,AS 为我们携带了大量的模板,这里不一一例举,就抽出几个常用的来做演示。
用法:
- 全关键字型:关键字–>TAB | 回车
- 后缀关键型:使用对象.关键字–>TAB | 回车
自带 Live Templates演示
fbc
快速创建 findViewById,以前我们要获取一个控件,我们会用以下方式:
![img_f45929f92163d393d8c5717862f1e231.gif](https://yqfile.alicdn.com/img_f45929f92163d393d8c5717862f1e231.gif)
但是如果我们用 fbc + .var 就完全不一样了:
![img_8503acda00a1d4db2d08715423da564d.gif](https://yqfile.alicdn.com/img_8503acda00a1d4db2d08715423da564d.gif)
快速 for each
快捷键:
- list.for + 回车
以往要写一个for循环变量集合的每一个元素:
![img_ab34caecfecd8ecf480bb112060fffc7.gif](https://yqfile.alicdn.com/img_ab34caecfecd8ecf480bb112060fffc7.gif)
但是,在AS中就不一样了,来看看:
![img_597f0aee519326836e6c4d300e1932e9.gif](https://yqfile.alicdn.com/img_597f0aee519326836e6c4d300e1932e9.gif)
当然还有增强foreach
![img_403b41aba1f42abc07b7783c851e7cfa.gif](https://yqfile.alicdn.com/img_403b41aba1f42abc07b7783c851e7cfa.gif)
常量定义
快速创建产量,省去了 public static final 这几个关键词的定义。
快捷键:
- const + TAB
用法:
![img_6966da15ce56ae236d6556a496bc9d2e.gif](https://yqfile.alicdn.com/img_6966da15ce56ae236d6556a496bc9d2e.gif)
Toast
快速创建一个toast.
快捷键:
- Toast + 选中回车
![img_908d88b566a82cac6b117f67e9dd24ae.gif](https://yqfile.alicdn.com/img_908d88b566a82cac6b117f67e9dd24ae.gif)
GONE 和 VISIBLE
快速显示和隐藏View
![img_d1bc493dbd62b79a72150c377578a4ee.gif](https://yqfile.alicdn.com/img_d1bc493dbd62b79a72150c377578a4ee.gif)
Log 相关
快速打印log:
![img_9c7dccf2acd893632bb9671321d87dd4.png](https://yqfile.alicdn.com/img_9c7dccf2acd893632bb9671321d87dd4.png?x-oss-process=image/resize,w_1400/format,webp)
![img_87cd245ff506699322c4cd530b56f358.gif](https://yqfile.alicdn.com/img_87cd245ff506699322c4cd530b56f358.gif)
表达式相关
比方说或 ==null 和 !=null 之类的:
![img_b2787b8e93d076f8c40060351058fd92.gif](https://yqfile.alicdn.com/img_b2787b8e93d076f8c40060351058fd92.gif)
其他
其他剩余的就不一一阐述了,大家可以去 Settings->Editor->Live Templates 下查看相应的 Templates,主要是懒,不想每一个快捷键都录制一遍。
简单自定义 Live Templates
SingleInstance 模板例子
新建 Live Templates:
![img_8279da3ea5622a10c45ac1c70f2f7824.png](https://yqfile.alicdn.com/img_8279da3ea5622a10c45ac1c70f2f7824.png?x-oss-process=image/resize,w_1400/format,webp)
补充:
![img_ff65edad17a783a03cf6769e2a9a15e1.png](https://yqfile.alicdn.com/img_ff65edad17a783a03cf6769e2a9a15e1.png?x-oss-process=image/resize,w_1400/format,webp)
Edit variables:
![img_ea0a1bdd16adb3d46d64b59efe80d781.png](https://yqfile.alicdn.com/img_ea0a1bdd16adb3d46d64b59efe80d781.png?x-oss-process=image/resize,w_1400/format,webp)
看看效果:
![img_1743777bf229f08e2fb0e928c7050e3e.gif](https://yqfile.alicdn.com/img_1743777bf229f08e2fb0e928c7050e3e.gif)
源码:
private static $class$ m$class$ = null;
private $class$(){}
public static $class$ getInstance() {
synchronized ($class$.class) {
if (m$class$ == null) {
m$class$ = new $class$();
}
}
return m$class$;
}
OnClickListener 例子模板
新建模板:
![img_177502553289b8ed8dee2fd394560429.png](https://yqfile.alicdn.com/img_177502553289b8ed8dee2fd394560429.png?x-oss-process=image/resize,w_1400/format,webp)
选择应用范围:
![img_ca58eaa605ee6046199b4be5cee838d7.png](https://yqfile.alicdn.com/img_ca58eaa605ee6046199b4be5cee838d7.png?x-oss-process=image/resize,w_1400/format,webp)
效果:
![img_9d3c5d6a57fdacaef6852f635a17157d.gif](https://yqfile.alicdn.com/img_9d3c5d6a57fdacaef6852f635a17157d.gif)
源码:
$VIEW$.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
最后
关于
关于自定义Live Templates还有很多的内容没有写,现在只是简单的有个概念,后面会还有一个章节专门讲自定义Live Templates和踩到的坑。
当然也可以 直接查看官方文档进行踩坑:
- 参考:https://www.jetbrains.com/help/idea/2016.3/creating-and-editing-template-variables.html
- 参考:https://www.jetbrains.com/help/idea/template-variables.html
另外就是填一下上两篇的坑,一般我在配置好快捷键,字体颜色等都会导出一个 setting.jar 的配置文件存放起来,每次更换新的电脑直接进行导入就行了,不用再重新设置一边。
![img_a89acb46ff6e9a285f8626f7d1e9beb3.png](https://yqfile.alicdn.com/img_a89acb46ff6e9a285f8626f7d1e9beb3.png?x-oss-process=image/resize,w_1400/format,webp)
其他
未完待续、敬请期待!
我的博客地址
![img_1ee92a858822d3b1d90a45e40e7b1042.jpe](https://yqfile.alicdn.com/img_1ee92a858822d3b1d90a45e40e7b1042.jpeg?x-oss-process=image/resize,w_1400/format,webp)