AndroidStudio筆記(3)提升效率的 Live Templates
- AS 自帶的 Live Templates
- 自帶 Live Templates演示
- 簡單自定義 Live Templates
- SingleInstance 模板例子
- OnClickListener 例子模板
前言
安卓開發者現如今主流的編譯器就是 Android+Studio/">Android Studio (以下簡稱AS),而 AS 是基於 IDEA 而定製化開發的編譯器。AS 為我們提供了大量能夠減少編碼量和編碼效率的功能,本文著重講解 AS 自帶的 Live Templates 和自定義 Live Templates。
正文
AS 自帶的 Live Templates
如何檢視 AS 的 Live Templates呢? 看圖:
從圖中可以看到,AS 為我們攜帶了大量的模板,這裡不一一例舉,就抽出幾個常用的來做演示。
用法:
- 全關鍵字型:關鍵字–>TAB | 回車
- 字尾關鍵型:使用物件.關鍵字–>TAB | 回車
自帶 Live Templates演示
fbc
快速建立 findViewById,以前我們要獲取一個控制元件,我們會用以下方式:
但是如果我們用 fbc + .var 就完全不一樣了:
快速 for each
快捷鍵:
- list.for + 回車
以往要寫一個for迴圈變數集合的每一個元素:
但是,在AS中就不一樣了,來看看:
當然還有增強foreach
常量定義
快速建立產量,省去了public static final 這幾個關鍵詞的定義。
快捷鍵:
- const + TAB
用法:
Toast
快速建立一個toast.
快捷鍵:
- Toast + 選中回車
GONE 和 VISIBLE
快速顯示和隱藏View
Log 相關
快速列印log:
表示式相關
比方說或 ==null 和 !=null 之類的:
其他
其他剩餘的就不一一闡述了,大家可以去Settings->Editor->Live Templates 下檢視相應的 Templates,主要是懶,不想每一個快捷鍵都錄製一遍。
簡單自定義 Live Templates
SingleInstance 模板例子
新建 Live Templates:
補充:
Edit variables:
看看效果:
原始碼:
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 例子模板
新建模板:
選擇應用範圍:
效果:
原始碼:
$VIEW$.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
最後
關於
關於自定義Live Templates還有很多的內容沒有寫,現在只是簡單的有個概念,後面會還有一個章節專門講自定義Live Templates和踩到的坑。
當然也可以 直接檢視官方文件進行踩坑:
- 參考:ofollow,noindex">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 的配置檔案存放起來,每次更換新的電腦直接進行匯入就行了,不用再重新設定一邊。
## 其他
未完待續、敬請期待!
我的部落格地址