设置位置
setting->Editor->Live Templates
![](https://i-blog.csdnimg.cn/blog_migrate/5c812257dc8997febb8a6878ee289f29.webp?x-image-process=image/format,png)
创建一个Live Templates
![](https://i-blog.csdnimg.cn/blog_migrate/b1a83cba2de48d4d216770c522f4bf3a.webp?x-image-process=image/format,png)
击点右边的 + 号出现如上界面
如我们想把findViewbyid创建一个Live Templates,可以先把代码cv到Template text里,然后用$...$ 替换等待输入的变量替换后如下
($cast$) findViewById(R.id.$res$);
记得要先点击下面的Define定义这个Templates使用的地方,一般全部勾选就可以了
然后就可以点击edit variables对等输入变量进行编辑了
![](https://i-blog.csdnimg.cn/blog_migrate/461b225064b24e5399954c7400327db1.webp?x-image-process=image/format,png)
Name 你定义的所有$..$
Expression 为待输入变量
Default value 赋值默认值
Skip if defined是否跳过编辑
上面Expression使用expectedType()就可以根据我们前面定义的View自动转换,当前Expression还有很多函数可以使用具体可以查看如下官方说明:
https://www.jetbrains.com/help/idea/2016.1/live-template-variables.html
结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/bea89d46ddb75f0a43a7ae5ea6bed081.webp?x-image-process=image/format,png)
在定义一个常见的LOG
![](https://i-blog.csdnimg.cn/blog_migrate/137f42705da5a1cf8f947d9046aa3863.webp?x-image-process=image/format,png)
基本跟findviewById差不多,只是Expression改为className,OK结果如下
![](https://i-blog.csdnimg.cn/blog_migrate/efde963ae3a48e47df5a18885aa6b118.webp?x-image-process=image/format,png)
快速导入一个Live Templates
如果你跟我一样那么懒,自己慢慢加就太慢了,大神早已写好了一些常用的Live Templates,直接导入使用就可以了,地址如下:
https://github.com/keyboardsurfer/idea-live-templates
直接复制到你的 Android Studio config\templates目录下
我的在这里C:\Users\Administrator\.AndroidStudio2.0\config\templates
按下ctrl + J 就可以快速查看Live Templates了
![](https://i-blog.csdnimg.cn/blog_migrate/e3f317f69da05cd7f628d4294a8a2929.webp?x-image-process=image/format,png)
参考文章:
http://blog.csdn.net/DesmondJ/article/details/47017205
https://www.bignerdranch.com/blog/android-studio-live-templates/
https://github.com/keyboardsurfer/idea-live-templates
https://www.jetbrains.com/help/idea/2016.1/live-template-variables.html?origin=old_help