感謝:CSDN的yissan
感謝:簡書的咖枯
我只是搬運工。文章最后附上他們文章網址。
------------------------------我是代碼界的搬運工--------------------------------------
概述
現如今開發越來越追求效率和節奏,節省出時間做更多的事情,除了開發技術上的封裝等,開發工具的使用技巧也是很重要的,今天就根據自己的經驗來給大家介紹一下Android Studio快速開發之道。
Postfix completion
Postfix completion 是IntelliJ IDEA很早就有的功能,該功能基于已經輸入的表達式和你添加的后綴來實現另一個你想要的表達式。例如在布爾表達式之后加上后綴if就是if語句 。
常用后綴介紹
下面介紹一下個人工作中覺得比較常用的幾個后綴。
-
cast
當一個變量需要類型轉換時,你是不是經常多次回退到變量名前面,然后括號再輸入類型,現在你只需要.cast之后瀟灑的輸入類型。
-
if
對于if語句你有更好的選擇,.if
-
else
else就是if取反,就是那么快
-
field
假設你的代碼寫了很長,突然在1000行的時候要給一個變量賦值并且聲明為全局變量,難道要返回去重新聲明嗎,簡直麻煩。有了field,在哪都可以。
-
fori
簡單快速for循環,就是那么吊
-
for
for循環第二式,增強for循環(使用.iter也是同樣的效果)
-
forr
for循環第三式,反向for循環 .forr
-
null
快速判空
-
nn/notnull
快速判斷非空 .nn,與.notnull效果一致
-
switch
.switch 通過字符串枚舉等類型的對象直接生成switch語句
-
inst/instantsof
快速進行instanof判斷 并進行類型轉換
-
var
快速為一個表達式生成變量
-
par
快速為一個表達式添加括號
-
log
快速log,想輸出的內容.log即可
-
toast
快速toast,想輸出的內容.toast即可
除了以上的后綴語句,Android Studio還有很多其它的后綴語句,由于個人覺得不是很常用,所以沒有提到,大家可以打開setting-Editor-General-Postfix Completion查看更多用法。
下面貼一張Postfix Completion的圖
Live Templates
讓您插入頻繁使用的或自定義的代碼結構,快速、高效、準確地構建到您的源代碼文件中。
我們可以直接打開Settings/Preferences 對話框 選中Editor下的Live Templates進行查看
通過上圖大家可以看到Android Studio已經為我們提供了大量的模版。
系統模版簡單認識
這里我們通過系統提供的一個模版來對Live Templates簡單認識一下。
上圖中Android為分組名,該分組下系統提供了許多模版。
我們可以在右上角點擊+進行添加自己的group或者模版
Abbreviation就是模版的縮寫,定義了縮寫之后就能直接輸入縮寫得到模版具體內容。
Description在輸入的時候會有一個附帶的描述信息進行提示。
Tempplate text中的就是具體內容,具體內容中可以通過variable
來進行變量定義,上圖中的模版定義了的name和value兩個變量。
可以看到 name指定了默認值NAME,value是一個1000以內的隨機數。關于變量的編輯里面有很多細節,由于篇幅原因,不再這里詳細討論,后續我會寫一篇博客對live templelates各個功能進行詳細介紹。
繼續剛才說到第一個模版,左下角有一個change按鈕,點擊之后可以指定該模版的作用范圍。
基本介紹就完畢了,看下效果
系統提供的模版很多并且大多數都很有用,我就不一一列舉了,大家可以自己查看Live Templates找到自己合適的使用,開始的時候可能不適應,之后,你會發現,你越來越快。
自定義模版
了解了Live Templates之后,我發現我經常使用butterknife的時候會寫onClick,但是每次都是一樣的代碼,這時候我就可以自定義一個模版,請看下圖:
自定義了一個分組 custom,并且在該分組下定義了一個縮寫為onclick的模版
模版內容就是一個butterknife注解修飾的click方法,并且聲明了一個個變量id。
點擊Edit variables如下圖
指定了 Expression為completeSmart(),表示該變量會智能型的完成調用(光標會出現在變量位置)
具體效果如下:
這里為了方便 隨便賦值為 activity的資源id了。
- 小技巧1
在使用Live Templates我如果正在寫代碼,寫了半個小時,我發現此時正在鞋一段代碼已經寫了幾次并且很可能接下來也會寫。難道我們copy下來再 打開setting找到Live Templates去添加嗎?你完全有更好的選擇!
選中你的代碼,點擊菜單攔Tools->Save as Live templates 之后直接指定縮寫等參數保存即可。
或者 打開設置 keymap,輸入過濾條件live template,找到Save as Live templates設置快捷鍵
[圖片上傳中。。。(24)]
這里我設置為cmd+shift+s
就不用點菜單了,選中代碼直接cmd+shift+s
[圖片上傳中。。。(25)]
ps:由于我之前設置過快捷鍵,還以為有默認的快捷鍵,想看看默認快捷鍵寫下來,所以點了下reset,發現這個功能沒有提供默認快捷鍵,然后最悲劇的來了,發現reset之后我沒有點apply尼瑪居然也生效了,所有我修改過的快捷鍵全沒拉。。。。。 - 小技巧2
加入你想使用一個模版,但是忘記了它的縮寫,此時怎么辦呢? 快速的cmd+j,windows電腦我不清楚,大家設置技巧1的時候會看到一個insert live templates,就可以看到快捷鍵了。該功能可以提示出所有適合當前場景的模版。
當然Live Templates的知識很有很多,比如定義變量里的expression有好多,各自代表什么意思,但是這些不是本篇文章的重點,所以就不做討論了。
快捷鍵
Android Studio提供的快捷鍵功能非常強大,個人覺得只要你對快捷鍵熟悉至極,從打開Android Studio那一刻,你就可以把鼠標扔了。。。
個人常用快捷鍵介紹
先來一波常用功能快捷鍵效果演示,沒圖我也是不能服眾啊(由于快捷鍵很多這里只做部分演示),ps:這里的快捷鍵是基于as自帶的快捷鍵類型,如果你選擇了其它的快捷鍵類型(如eclipse),你只需要打開設置,keymap,通過輸入過濾條件找到對應的功能查看快捷鍵。
以下格式為
描述/Windows或Linux的快捷鍵/Mac 快捷鍵
介紹
圖片
- Find action (F3/Command + G)
查找as所有動作功能(超級強大)
- Find next (F3/Command + G)
查找下一個
-
Find previous
查找上一個
-
Find class(Control + N/Command + O)
直接通過類名過濾打開類文件,有了這個基本我很少用鼠標去找類文件了。
-
Duplicate current line or selection(Control + D/Command + D)
快速產生一個單行或者選中行的copy
-
Go to implementations( Control + Alt + B/Command + Alt + B)
找到當前接口的實現類,或接口方法的實現方法,在mvp模式里最常用
-
Extract method( Control + Alt + M/Command + Option + M)
為選中代碼生產一個方法,超級智能,甚至能識別處是否要參數和返回值,如果有多個相似代碼,會提示是否要統一替換,吊到沒朋友。
Extract variable(Control + Alt + V/Command + Option + V)
為選中表達式生成臨時變量,如果有多個地方會提示是否需要統一替換。
-
Extract field(Control + Alt + F/Command + Option + F)
為選中表達式生成全局變量,如果有多個地方會提示是否需要統一替換。
-
Next highlighted error(F2 / F2)
你一定有過在一個很長代碼文件中找一個錯誤的時候,很麻煩,有了這個功能,光標直接移到錯誤的地方。
篇幅有限,就介紹這么多了,其它的系統快捷鍵我以表格的形式整理下來,許多功能需要自己去嘗試才能發現它的強大。
系統默認快捷鍵
以表格形式列出個人覺得比較常用得系統快捷鍵,ps:這里列出得是系統默認的快捷鍵,如果你快捷鍵類型選定為eclipse或者其它,你需要找到對應的描述進行查看或者修改。
- 普通類型
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Open settings dialogue | 快速打開設置 | Control + Alt + S | Command + , (comma) |
Open project structure dialog | 打開項目結構 | Control + Alt + Shift + S | Command + ; (semicolon) |
Switch between tabs and tool window | tab的切換 | Control + Alt + Shift + S | Command + ; (semicolon) |
- 追蹤和搜索
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Find | 查找 | Control + Alt + S | Command + , (comma) |
Search everything (including code and menus) | 查找所有 | 點擊shift兩次即可搜索 | 點擊shift兩次即可搜索 |
Find next | 順序向下查找 | F3 | Command + G |
Find action | 查找功能,超級強大,as的所有功能可以通過該功能來查找 | Control + Shift + A | Command + Shift + A |
Find class | 快速找到類并打開文件 | Control + N | Command + O |
Find file (instead of class) | 快速查找文件 | Control + Shift + N | Command + Shift + O |
Find in path | 在項目下的所有路徑查找 | Control + Shift + F | Command + Shift + F |
Recently opened files pop-up | 列出最近打開的文件 | F3Control + E | Command + E |
Recently edited files pop-up | 列出最近編輯過的文件 | Control + Shift + E | Command + Shift + E |
Go to last edit location | 回到最近一次編輯的位置 | Control + Shift + Backspace | Command + Shift + Backspace |
Close active editor tab | 關閉當前編輯的文件 | Control + F4 | Command + W |
Open type hierarchy | 打開類型層次關系 | Control + H | Control + H |
Open method hierarchy | 打開方法層次關系 | Control + Shift + H | Command + Shift + H |
Open call hierarchy | 打開調用層次關系 | Control + Alt + H | Control + Option + H |
- 寫代碼常用快捷鍵
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Generate code (getters, setters, constructors, hashCode/equals, toString, new file, new class) | 快速生成各種方法 | Alt + Insert | Command + N |
Override methods | 顯示所有可以重寫的方法 | Control + O | Control + O |
Implement methods | 顯示所有可以實現的方法 | Control + I | Control + I |
Surround with (if…else / try…catch / etc.) | 快速為語句生成if else,try catch for循環等 | Control + Alt + T | Command + Option + T |
Delete line at caret | 刪除光標所在行 | Control + Y | Command + Backspace |
Duplicate current line or selection | 快速產生新的當前行,或者新的多行代碼 | Control + D | Command + D |
Quick documentation lookup | 快速查看簡要文檔 | Control + P | Command + P |
Go to declaration (directly) | 找到變量或方法聲明處 | Control + B or Control + Click | Command + B or Command + Click |
Go to implementations | 找到一個接口的實現類,接口方法的是實現處 | Control + Alt + B | Command + Alt + B |
Go to super-method/super-class | 找到父類或父類方法 | Control + U | Command + U |
Toggle project tool window visibility | 顯示或隱藏as左邊的項目窗口 | Alt + 1 | Command + 1 |
Comment/uncomment with line comment | 快速單行注釋或去除注釋 | Control + / | Command + / |
Comment/uncomment with block comment | 多行注視或除去多行注釋 | Command + Shift + / | Command + Shift + / |
Project quick fix (show intention actions and quick fixes) | 給出當前問題的多種解決方法(該方法非常常用) | Alt + Enter | Option + Enter |
Reformat code | 格式化代碼 | Control + Alt + L | Command + Option + L |
Next/previous highlighted error | 找到下一個/上一個錯誤 | F2 / Shift + F2 | F2 / Shift + F2 |
- 編譯運行
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Build | 編譯 | Control + F9 | Command + F9 |
Build and run | 編譯 | Shift + F10 | Control + R |
- 調試
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Debug | Shift + F9 | Shift + F9 | |
Step over | F8 | F8 | |
Step into | F7 | F7 | |
Smart step into | Shift + F7 | Shift + F7 | |
Resume program | F9 | Command + Option + R |
- 修改
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Safe delete | 安全刪除 | Alt + Delete | Command + Delete |
Extract method | 提取方法 | Control + Alt + M | Command + Option + M |
Extract method | 提取方法 | Control + Alt + V | Command + Option + V |
Extract variable | 提取變量 | Control + Alt + V | Command + Option + V |
Extract field | 提取全局變量 | Control + Alt + F | Command + Option + F |
Extract constant | 提取常量 | Control + Alt + C | Command + Option + C |
Extract parameter | 提取參數 | Control + Alt + P | Command + Option + P |
- 版本控制/本地歷史
description | 解釋 | Windows/Linux | Mac |
---|---|---|---|
Commit project to VCS | Control + K | Command + K | |
Update project from VCS | Control + T | Command + T | |
View recent changes | 查看最近修改 | Alt + Shift + C | Option + Shift + C |
Open VCS popup | 打開版本控制設置窗口 | Alt + ` (backquote) | Control + V |
擴展
- 自定義快捷鍵
看到這里相信大家對快捷鍵有了更深的認識,我們可以通過簡單的幾個鍵就實現強大的功能,但是有時候許多系統提供的快捷鍵也許和你本身系統的快捷鍵沖突,或者說某些強大的功能as并沒有提供你快捷鍵,此時就涉及到快捷鍵的自定義(修改或者添加)。
比如上面提到的 找到并高亮錯誤代碼 as提供的是F2鍵,但是在Mac上是功能鍵并且我也想保留,我就修改為了command+shift+2
關于快捷鍵的自定義我就不細提了,大家百度一下有很多文章。 - 發現更多功能
keyMap里列出的功能眾多,篇幅和精力有限,不能一一列出, 各位有時間的時候可以打開設置->KeyMap,去看里面的快捷鍵,不確定的就自己親自按了試一下,你會發現原來可以那么方便。
Android Studio插件
1.通過WiFi調試手機:Android Wifi ADB、ADB WiFI
2.ButterKnife 注解生成器:ButterKnife Zelezny
3.Selector文件生成:SelectorChapek
4.Gson自動化:GsonFormat
5.Parcelable自動化:Android Parcelable Code Generator
6.生成ViewHolder和findView方法:AndroidCodeGenerator
7.代碼行數統計:statistic
8.代碼檢索:codota
9.模擬器:Genymotion
10.快速進行Log、Toast、isEmpty的代碼書寫:postfix
11.Android Studio 翻譯插件,可以將英文翻譯為中文:ECTranslation:https://github.com/Skykai521/ECTranslation
12.可用于快速定位代碼:CodeGlance
13.可以根據Activity或者fragment的生命周期對其生命周期方法位置進行先后排序:Lifecycle Sorter
14.通過FindBugs幫你找到隱藏的bug及不好的做法:FindBugs-IDEA
15.該網站搜集了大量的代碼,號稱超過700W的代碼實例:codota
16.檢查代碼風格的插件:checkstyle-idea
17.一個針對API 23,可在Activity/Fragment中快速生成Runtime Permissions代碼的插件:PermissionsDispatcher
18.生成Material Design Icon的插件:Android Material Design Icon Generator
19.Material Theme配色方案:Material Theme UI
20.Android 高效開發調試神器:JRebel:https://zeroturnaround.com/
21.號稱Android平臺上的秒級編譯方案:Freeline:https://yq.aliyun.com/articles/59122?spm=5176.8091938.0.0.1Bw3mU
安裝插件方法:打開 File -> Settings -> Plugins -> Browse Repositories ,輸入插件名搜索,直接點擊"Install"安裝。如果搜索不到的話可以去這里下載下來后,選擇"Install plugin from disk"進行離線安裝。
----------------------------------搬運結束---------------------------------------------
最后附上二位文章網址:
Android Studio快速開發之道
持續收集Android Studio好用的插件~~只選對的!