什么是Live Templates
以我的理解,Live Templates是代碼模板的快捷定義,如常見的,你定義了findViewById(R.id.)的模塊代碼的模板為fvd,那么以后我們按fv就可以快速的調用定義好的模板了!熟練的使用Live Templates將大大提到我們的編程效率!這個例子下面也有講到,那么先看看怎么定義一個Live Templates
設置位置
setting->Editor->Live Templates
創建一個Live Templates
擊點右邊的 + 號出現如上界面
如我們想把findViewbyid創建一個Live Templates,可以先把代碼cv到Template text里,然后用$...$ 替換等待輸入的變量替換后如下
($cast$) findViewById(R.id.$res$);
記得要先點擊下面的Define定義這個Templates使用的地方,一般全部勾選就可以了
然后就可以點擊edit variables對等輸入變量進行編輯了
Name? 你定義的所有$..$
Expression ?為待輸入變量
Default value? 賦值默認值
Skip if defined是否跳過編輯
上面Expression使用expectedType()就可以根據我們前面定義的View自動轉換,當前Expression還有很多函數可以使用具體可以查看如下官方說明:
https://www.jetbrains.com/help/idea/2016.1/live-template-variables.html
結果如下:
在定義一個常見的LOG
基本跟findviewById差不多,只是Expression改為className,OK結果如下
快速導入一個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了
參考文章:
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