Android开发Tips

jopen 9年前
   <blockquote>     <p>欢迎Follow我的<a href="/misc/goto?guid=4959652813041333444" target="_blank">GitHub</a>, 关注我的<a href="/misc/goto?guid=4959652813131126148" target="_blank">简书</a>.</p>    </blockquote>    <p>介绍一些, 在Android开发中, 会经常使用的小知识点.</p>    <div class="image-package" href="https://simg.open-open.com/show/0b107f5a380e9f0936a64860ecf7c8ef.jpg">    <img src="https://simg.open-open.com/show/0b107f5a380e9f0936a64860ecf7c8ef.jpg" width="660" height="387" data-original-src="https://simg.open-open.com/show/c50136a5d1985288024b5c55bdc79205.jpg" />     <br />     <div class="image-caption">     Android     </div>    </div>    <h2>1. Download文件夹</h2>    <p>绝对路径</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/storage/emulated/0/Download/xxx</pre>    <p>遍历</p>    <pre class="brush:java; toolbar: true; auto-links: false;">        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());         }</pre>    <h2>2. ButterKnife多参数</h2>    <p>绑定多个参数</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    @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     })</pre>    <h2>3. submodule的使用方法</h2>    <p>submodule与git可以保持实时同步.<br /> 添加</p>    <pre class="brush:java; toolbar: true; auto-links: false;">git submodule add https://github.com/SpikeKing/DroidPlugin.git DroidPlugin</pre>    <p>使用</p>    <pre class="brush:java; toolbar: true; auto-links: false;">git submodule update --init --recursive</pre>    <p>导入, 路径多于一个, 前面不添加冒号(:).</p>    <pre class="brush:java; toolbar: true; auto-links: false;">include ':app', 'DroidPlugin:project:Libraries:DroidPlugin'</pre>    <p>引用</p>    <pre class="brush:java; toolbar: true; auto-links: false;">compile project(':DroidPlugin:project:Libraries:DroidPlugin')</pre>    <h2>4. 更新Github的Fork库</h2>    <p><a href="/misc/goto?guid=4959653517403310014" target="_blank">参考</a></p>    <h2>5. 检测App是否安装</h2>    <p>使用PackageManager.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">// 检查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; }</pre>    <h2>6. Canvas重绘</h2>    <p><a href="/misc/goto?guid=4959653517493633276" target="_blank">invalidate()</a>. <a href="/misc/goto?guid=4959653517577236441" target="_blank">参考</a>.</p>    <h2>7. 按钮的默认点击效果</h2>    <p>波纹效果(5.0+), 阴影效果(5.0-).</p>    <pre class="brush:java; toolbar: true; auto-links: false;">android:background="?android:attr/selectableItemBackground"</pre>    <p>继承样式</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    <!--按钮-->     <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></pre>    <blockquote>     <p>注意: @android:style/ButtonBar</p>    </blockquote>    <h2>8. Proguard去除Log信息</h2>    <p>默认删除log.i, .v, 可以指定删除.d, .e. <a href="/misc/goto?guid=4959653517662834174" target="_blank">参考</a>.</p>    <pre class="brush:java; toolbar: true; auto-links: false;"># 删除Log -assumenosideeffects class android.util.Log { *; } -assumenosideeffects class android.util.Log {     public static *** d(...);     public static *** e(...); }</pre>    <h2>9. 简化数据库的使用</h2>    <p>在使用数据库时, 操作有些复杂, Sugar库简化使用方法. <a href="/misc/goto?guid=4958848076971018590" target="_blank">参考</a>.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">compile 'com.github.satyan:sugar:1.3'</pre>    <h2>10. 点击被填充链接的EditView.</h2>    <p>通过在结尾处添加一个不占位的空格("\u200B").</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    // 设置可以点击和编辑的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>    <p>OK. That's all!</p>    <p>来自: <a href="/misc/goto?guid=4959653517774516558" rel="nofollow" target="_blank">http://www.jianshu.com/p/6b663027c371</a></p>