Android Studio打造自己的Live Templates

什么是Live Templates

以我的理解,Live Templates是代碼模板的快捷定義,如常見的,你定義了findViewById(R.id.)的模塊代碼的模板為fvd,那么以后我們按fv就可以快速的調(diào)用定義好的模板了!熟練的使用Live Templates將大大提到我們的編程效率!這個(gè)例子下面也有講到,那么先看看怎么定義一個(gè)Live Templates

設(shè)置位置

setting->Editor->Live Templates

創(chuàng)建一個(gè)Live Templates

擊點(diǎn)右邊的 + 號(hào)出現(xiàn)如上界面

如我們想把findViewbyid創(chuàng)建一個(gè)Live Templates,可以先把代碼cv到Template text里,然后用$...$ 替換等待輸入的變量替換后如下

($cast$) findViewById(R.id.$res$);

記得要先點(diǎn)擊下面的Define定義這個(gè)Templates使用的地方,一般全部勾選就可以了

然后就可以點(diǎn)擊edit variables對(duì)等輸入變量進(jìn)行編輯了

Name? 你定義的所有$..$

Expression ?為待輸入變量

Default value? 賦值默認(rèn)值

Skip if defined是否跳過編輯


上面Expression使用expectedType()就可以根據(jù)我們前面定義的View自動(dòng)轉(zhuǎn)換,當(dāng)前Expression還有很多函數(shù)可以使用具體可以查看如下官方說明:

https://www.jetbrains.com/help/idea/2016.1/live-template-variables.html

結(jié)果如下:

在定義一個(gè)常見的LOG

基本跟findviewById差不多,只是Expression改為className,OK結(jié)果如下

快速導(dǎo)入一個(gè)Live Templates

如果你跟我一樣那么懶,自己慢慢加就太慢了,大神早已寫好了一些常用的Live Templates,直接導(dǎo)入使用就可以了,地址如下:

https://github.com/keyboardsurfer/idea-live-templates

直接復(fù)制到你的 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容