天天看點

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

使用Android Studio一段時間了,還有很多小技巧沒有掌握。比如:平常将字元串整理到string.xml中,都是手動的去複制字元串到string.xml中,然後再回來修改引用該字元串的代碼,這樣很low.今天發現了一個小技巧可以輕松整理字元串到string.xml中,在這裡分享出來。

1、如下面的布局檔案,一般寫布局檔案的時候都是直接把字元串寫到裡面,之後再整理到string.xml中:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

這個時候會有提示,要将該字元串整理到string.xml中,為了友善I18N國際化處理,如下圖所示:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

2、然後選中字元串“測試Alt+Enter”,同時按住Alt+Enter,即可看到彈出選擇的對話款,選擇其中的“Extract string resource”,如下圖所示:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

選擇其中的“Extract string resource”,出現如下圖所示的對話框

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

然後可以修改該字元串在string.xml中的key值,如下圖所示:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

修改完後,點選“OK”按鈕,就會自動修改代碼為如下所示的代碼:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

同時,在string.xml檔案中也會自動生成相應的代碼,如下圖所示:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

和在布局檔案中寫的字元串,整理到string.xml中的方式是一樣的,也是使用Alt + Enter快捷鍵進行處理,如下圖所示:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

使用Alt + Enter快捷鍵,然後彈出如下的對話框

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

選擇其中的“Extract string resource”, 然後彈出如下圖所示的對話框,然後修改該字元串在string.xml中的key值

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

點選“OK”按鈕,然後就會自動修改代碼為如下所示的代碼:

【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中
【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中
作者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
【我的Android進階之旅】Android Studio如何輕松整理字元串到string.xml中1、在布局檔案中寫的字元串,整理到string.xml中2、在Java檔案中寫的字元串,整理到string.xml中

繼續閱讀