Android開發Tips
介紹一些, 在Android開發中, 會經常使用的小知識點.

Android
</div>
1. Download文件夾
絕對路徑
/storage/emulated/0/Download/xxx
遍歷
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File[] files = file.listFiles(); for (int i = 0; i < files.length; ++i) { Log.e(TAG, files[i].getAbsolutePath()); }
2. ButterKnife多參數
綁定多個參數
@OnClick({ R.id.dialog_dau_share_wx, R.id.dialog_dau_share_wx_timeline, R.id.dialog_dau_share_weibo, R.id.dialog_dau_share_qq })
3. submodule的使用方法
submodule與git可以保持實時同步.
添加
git submodule add https://github.com/SpikeKing/DroidPlugin.git DroidPlugin
使用
git submodule update --init --recursive
導入, 路徑多于一個, 前面不添加冒號(:).
include ':app', 'DroidPlugin:project:Libraries:DroidPlugin'
引用
compile project(':DroidPlugin:project:Libraries:DroidPlugin')
4. 更新Github的Fork庫
5. 檢測App是否安裝
使用PackageManager.
// 檢查App是否安裝 private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; }
6. Canvas重繪
7. 按鈕的默認點擊效果
波紋效果(5.0+), 陰影效果(5.0-).
android:background="?android:attr/selectableItemBackground"
繼承樣式
<!--按鈕--> <style name="PersonInfoButton" parent="@android:style/ButtonBar"> <item name="android:layout_width">@dimen/d80dp</item> <item name="android:layout_height">@dimen/d32dp</item> <item name="android:textSize">@dimen/d14sp</item> </style>
注意: @android:style/ButtonBar
8. Proguard去除Log信息
默認刪除log.i, .v, 可以指定刪除.d, .e. 參考.
# 刪除Log -assumenosideeffects class android.util.Log { *; } -assumenosideeffects class android.util.Log { public static *** d(...); public static *** e(...); }
9. 簡化數據庫的使用
在使用數據庫時, 操作有些復雜, Sugar庫簡化使用方法. 參考.
compile 'com.github.satyan:sugar:1.3'
10. 點擊被填充鏈接的EditView.
通過在結尾處添加一個不占位的空格("\u200B").
// 設置可以點擊和編輯的EditText private void setEditClickable() { mEtEditText.setMovementMethod(LinkMovementMethod.getInstance()); Spannable spannable = new SpannableString("http://www.baidu.com"); Linkify.addLinks(spannable, Linkify.WEB_URLS);// 添加了零寬度空格(?\u200B???), 才可以點擊到最后的位置, 否則會觸發鏈接 CharSequence text = TextUtils.concat(spannable, "\u200B"); mEtEditText.setText(text); }</pre>
OK. That's all!
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!