Android開發Tips

來源:互聯網
上載者:User

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!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.