Android開發Tips
介紹一些, 在Android開發中, 會經常使用的小知識點.
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重繪
invalidate(). 參考.
7. 按鈕的預設點擊效果
波紋效果(5.0+), 陰影製作效果(5.0-).
android:background="?android:attr/selectableItemBackground"
繼承樣式
注意: @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); }
OK. That’s all!