前言
轉眼已經畢業一年了,工作一年還是學到了很多東西,但是由于公司維護的老項目,目前還是用eclipse開發,看着android studio 2.0 出來了,還是很想去嘗試的,另外很多高版本的API,github上的開源控件以及Material Design,于是我就決定做個簡單的應用,來熟悉一下android studio 以及對我來說這些比較新穎的東西。是以梳理一下自己的開發過程和參考的文章。
效果圖
先看一下效果圖吧,具體的可以去應用寶下載下傳
賬号管家下載下傳位址

整體思路
于是我想到做一個賬号管家的應用,來管理生活工作中亂七八糟的賬号。可是這個真的也不好做,誰放心把賬号密碼交給一個不知名的app呢,這裡我做了一些防護。
- 為了使用者放心使用,這是一個單機應用。
- 可以使用不可逆加密的盡量使用不可逆加密。(比如登入時候的解鎖密碼,不需要展示的都使用不可逆加密)
- 資料庫不可備份
- 代碼混淆
主要的技術
1.登入
可以是密碼登入或者九宮格解鎖(用到的是github上的PatternLock,有興趣的可以自己去看一下)
2.應用的首頁
還是采用github上的FlowingDrawer 控件(帶手勢流動效果側滑菜單github對于如何使用說的很清楚)抽屜導航NavigationView,可以參考一下(上面有效果圖):
下面是Navigation View的使用的參考
Navigation View的使用
3.RecyclerView展示
是用RecyclerView的瀑布流來展示,item是CardView,CardView是v7包下的。。遇到的坑可以參考
cardView開發中的小問題
4.使用到的輸入框
EditText+TextInputLayout,自帶動畫,非常炫酷。
以及EditText中的setError(“錯誤提示”)。如圖:
隻要把TextInputLayout 作為Edittext控件的父布局即可。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
可以看出來TextInputLayout是MD裡面提供的控件。
5.SnackBar:
Snackbar是Android Support Design Library庫中的一個控件,可以在螢幕底部快速彈出消息,比Toast更加好用。我在登入成功的時候預設做了一個SnackBar,用于提示系統時間。
參考:SnackBar使用
6.設定界面
像手機原生的設定頁面,這裡我用的是android 提供了一種設定界面的寫法:使用繼承PreferenceFragment 然後實作Preference.OnPreferenceChangeListener,Preference.OnPreferenceClickListener 監聽。在監聽的回調中處理自己的業務邏輯,界面如圖所示:
1. 首先要建立xml 包,在裡面建立preference.xml,選擇PreferenceScreen作為document的根。
2. 以下面布局為例,簡單說一下
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/preference_item"
android:title=""
>
<PreferenceCategory
android:layout="@layout/preference_category_widget"
android:title="@string/title_login_settings"
>
<CheckBoxPreference
android:key="@string/MODE_PATTERN_LOCK_MODE_VISIBLE"
android:layout="@layout/preference_item"
android:summaryOff="@string/pattern_lock_visible_off"
android:summaryOn="@string/pattern_lock_visible_on"
android:widgetLayout="@layout/switch_layout"
android:checked="true"
android:title="@string/pattern_lock_visible"/>
</PreferenceCategory>
</PreferenceScreen>
其中:
PreferenceCategory中的title為顯示偏好種類的名字,我這邊用的是‘登入設定’
CheckBoxPreference 中的key 為這個偏好的sp中存儲的key值,summaryOff 和 summaryOn表示開關操作是下方顯示的summary概述資訊 title 為上方标題。
widgetLayout 表示這個元件的布局 。我這邊使用的SwitchCompat控件 ,對應的switch_layout布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff="OFF"
android:textOn="ON" />
其中的android:id=”@android:id/checkbox” 表示為系統的checkbox,不可随意修改
在java代碼中:
public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener,Preference.OnPreferenceClickListener{
private CheckBoxPreference loginLockVisiablePre;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
loginLockVisiablePre = (CheckBoxPreference) getPreferenceManager()
.findPreference(getString(R.string.MODE_PATTERN_LOCK_MODE_VISIBLE));
loginLockVisiablePre.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(preference instanceof CheckBoxPreference) {
boolean checked = Boolean.valueOf(newValue.toString());
//擷取到對應的key 和value
Log.i("test",preference.getKey()+"=="+checked);
}
return true;
}
//如果有點選事件,可以在這裡處理
@Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
}
7.分享功能
分享應用和分享賬号密碼,這個需要閱讀微信開放平台去了解。大概流程是:
- 1.申請appId
- 2.內建微信開發sdk,隻用分享好像不需要聯網。在app包名下建立一個wxapi的包(如應用的包名是com.a.b,則必須有com.a.b.wxapi)需要建立一個WXEntryActivity的類。
- 3.調用sdk,發送請求,并且分享成功之後在WXEntryActivity中的onResp會回調到。我們這裡選擇finish目前的activity,防止顯示空白的activity。
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI mIWXAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIWXAPI = WXAPIFactory.createWXAPI(this, WXConstants.APP_ID);
mIWXAPI.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
finish();
}
}
其他的一些控件
很多v7包下的系統控件android.support.v7.app.AlertDialog;,ProgressDialog等等這些,其實已經做得很好看了,有機會還是多使用使用。
遇到的問題
1.application中theme主題的了解
建立一個空項目的時候,在清單檔案中的android:theme=”@style/AppTheme”,對應的
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="patternViewStyle">@style/PatternView.Light</item>
<!-- Or PatternView.Light, or your own style extending these two or not. -->
</style>
下面這張圖,可以幫我們更好的了解這些參數的意義:
我一開始建立的editText是粉紅色的,糾結了半天,原來是colorAccent 這個值設定的問題。。。
2.cardView顯示的問題
雖然上面的坑中有提到,但是我還是想再說一下:android 5.0以上和和以下是不一樣。
有問題的界面截圖:
主要是android 5.0前後的差異:在xml 中寫的
android:layout_margin=”@dimen/cardview_default_margin”,這個cardview_default_margin值在不同value包中的值需要不同:
我這裡是這樣寫的:
- res/values/dimens.xml中:
<dimen name="cardview_default_margin">0dp</dimen>
- res/values-v21/dimens.xml中
這樣就能解決這樣的顯示問題<dimen name="cardview_default_margin">8dp</dimen>
3.進入輸入界面,自動彈出軟鍵盤,這樣顯得不友好,處理方式:
- 可以在EditText前面放置一個看不到的LinearLayout
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/>
- 或者在他的父布局中加入
android:focusable="true" android:focusableInTouchMode="true"