1.注冊UI及驗證邏輯實現
1.1 布局
【說明】屬於業務邏輯,登陸的業務邏輯,新建sign,新建類;
【注意】如果在ScrollView布局中如果嵌套了其他的布局,則其他的布局的layout_height屬性應該為wrap_content;
【增加依賴】
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <android.support.v7.widget.Toolbar 9 android:layout_width="match_parent" 10 android:layout_height="?attr/actionBarSize" 11 android:background="@android:color/holo_orange_dark"> 12 13 <android.support.v7.widget.AppCompatTextView 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" 16 android:gravity="center" 17 android:text="注冊" 18 android:textColor="@android:color/white" 19 android:textSize="20sp" 20 tools:ignore="HardcodedText" /> 21 </android.support.v7.widget.Toolbar> 22 23 <android.support.v4.widget.NestedScrollView 24 android:layout_width="match_parent" 25 android:layout_height="match_parent"> 26 27 <android.support.v7.widget.LinearLayoutCompat 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:fitsSystemWindows="true" 31 android:orientation="vertical" 32 android:paddingLeft="24dp" 33 android:paddingRight="24dp" 34 android:paddingTop="56dp"> 35 36 <android.support.v7.widget.AppCompatImageView 37 android:layout_width="wrap_content" 38 android:layout_height="72dp" 39 android:layout_gravity="center_horizontal" 40 android:layout_marginBottom="24dp" 41 android:src="@mipmap/ic_launcher" /> 42 43 <!--姓名--> 44 <android.support.design.widget.TextInputLayout 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:layout_marginBottom="8dp" 48 android:layout_marginTop="8dp"> 49 50 <android.support.design.widget.TextInputEditText 51 android:id="@+id/edit_sign_up_name" 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:hint="姓名" 55 android:inputType="textPersonName" 56 tools:ignore="HardcodedText" /> 57 </android.support.design.widget.TextInputLayout> 58 59 <!--郵箱--> 60 <android.support.design.widget.TextInputLayout 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" 63 android:layout_marginBottom="8dp" 64 android:layout_marginTop="8dp"> 65 66 <android.support.design.widget.TextInputEditText 67 android:id="@+id/edit_sign_up_email" 68 android:layout_width="match_parent" 69 android:layout_height="wrap_content" 70 android:hint="郵箱" 71 android:inputType="textEmailAddress" 72 tools:ignore="HardcodedText" /> 73 </android.support.design.widget.TextInputLayout> 74 75 <!--手機號碼--> 76 <android.support.design.widget.TextInputLayout 77 android:layout_width="match_parent" 78 android:layout_height="wrap_content" 79 android:layout_marginBottom="8dp" 80 android:layout_marginTop="8dp"> 81 82 <android.support.design.widget.TextInputEditText 83 android:id="@+id/edit_sign_up_phone" 84 android:layout_width="match_parent" 85 android:layout_height="wrap_content" 86 android:hint="手機號碼" 87 android:inputType="phone" 88 tools:ignore="HardcodedText" /> 89 </android.support.design.widget.TextInputLayout> 90 91 <!--密碼--> 92 <android.support.design.widget.TextInputLayout 93 android:layout_width="match_parent" 94 android:layout_height="wrap_content" 95 android:layout_marginBottom="8dp" 96 android:layout_marginTop="8dp"> 97 98 <android.support.design.widget.TextInputEditText 99 android:id="@+id/edit_sign_up_password" 100 android:layout_width="match_parent" 101 android:layout_height="wrap_content" 102 android:hint="密碼" 103 android:inputType="textPassword" 104 tools:ignore="HardcodedText" /> 105 </android.support.design.widget.TextInputLayout> 106 107 <!--重復密碼--> 108 <android.support.design.widget.TextInputLayout 109 android:layout_width="match_parent" 110 android:layout_height="wrap_content" 111 android:layout_marginBottom="8dp" 112 android:layout_marginTop="8dp"> 113 114 <android.support.design.widget.TextInputEditText 115 android:id="@+id/edit_sign_up_re_password" 116 android:layout_width="match_parent" 117 android:layout_height="wrap_content" 118 android:hint="重復密碼" 119 android:inputType="textPassword" 120 tools:ignore="HardcodedText" /> 121 </android.support.design.widget.TextInputLayout> 122 123 <android.support.v7.widget.AppCompatButton 124 android:id="@+id/btn_sign_up" 125 android:layout_width="match_parent" 126 android:layout_height="wrap_content" 127 android:layout_marginBottom="24dp" 128 android:layout_marginTop="24dp" 129 android:background="@android:color/holo_orange_dark" 130 android:gravity="center" 131 android:padding="12dp" 132 android:text="注冊" 133 android:textColor="@android:color/white" 134 tools:ignore="HardcodedText" /> 135 136 <android.support.v7.widget.AppCompatTextView 137 android:id="@+id/tv_link_sign_in" 138 android:layout_width="match_parent" 139 android:layout_height="wrap_content" 140 android:layout_marginBottom="24dp" 141 android:gravity="center" 142 android:text="已經注冊了?請登錄" 143 android:textSize="16sp" 144 tools:ignore="HardcodedText" /> 145 146 </android.support.v7.widget.LinearLayoutCompat> 147 </android.support.v4.widget.NestedScrollView> 148 </android.support.v7.widget.LinearLayoutCompat>
1.2 注冊信息的驗證和邏輯
【布局控件的查找】
【輸入注冊信息的驗證】
1 public class SignUpDelegate extends LatteDelegate { 2 3 @BindView(R2.id.edit_sign_up_name) 4 TextInputEditText mName = null; 5 @BindView(R2.id.edit_sign_up_email) 6 TextInputEditText mEmail = null; 7 @BindView(R2.id.edit_sign_up_phone) 8 TextInputEditText mPhone = null; 9 @BindView(R2.id.edit_sign_up_password) 10 TextInputEditText mPassword = null; 11 @BindView(R2.id.edit_sign_up_re_password) 12 TextInputEditText mRePassword = null; 13 14 15 private boolean checkForm() { 16 final String name = mName.getText().toString(); 17 final String email = mEmail.getText().toString(); 18 final String phone = mPhone.getText().toString(); 19 final String password = mPassword.getText().toString(); 20 final String rePassword = mRePassword.getText().toString(); 21 22 boolean isPass = true; 23 24 if (name.isEmpty()) { 25 mName.setError("請輸入姓名"); 26 isPass = false; 27 } else { 28 mName.setError(null); 29 } 30 31 if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 32 mEmail.setError("錯誤的郵箱格式"); 33 isPass = false; 34 } else { 35 mEmail.setError(null); 36 } 37 38 if (phone.isEmpty() || phone.length() != 11) { 39 mPhone.setError("手機號碼錯誤"); 40 isPass = false; 41 } else { 42 mPhone.setError(null); 43 } 44 45 if (password.isEmpty() || password.length() < 6) { 46 mPassword.setError("請填寫至少6位數密碼"); 47 isPass = false; 48 } else { 49 mPassword.setError(null); 50 } 51 52 if (rePassword.isEmpty() || rePassword.length() < 6 || !(rePassword.equals(password))) { 53 mRePassword.setError("密碼驗證錯誤"); 54 isPass = false; 55 } else { 56 mRePassword.setError(null); 57 } 58 59 return isPass; 60 }
【測試】進入到注冊的界面;
2.登錄UI及驗證邏輯實現
2.1 布局
【源碼】layout/delegate_sign_in.xml,支持微信第三方登錄
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v7.widget.LinearLayoutCompat 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical"> 8 9 <android.support.v7.widget.Toolbar 10 android:layout_width="match_parent" 11 android:layout_height="?attr/actionBarSize" 12 android:background="@android:color/holo_orange_dark"> 13 14 <android.support.v7.widget.AppCompatTextView 15 android:layout_width="match_parent" 16 android:layout_height="match_parent" 17 android:gravity="center" 18 android:text="登錄" 19 android:textColor="@android:color/white" 20 android:textSize="20sp" 21 tools:ignore="HardcodedText" /> 22 </android.support.v7.widget.Toolbar> 23 24 <android.support.v4.widget.NestedScrollView 25 android:layout_width="match_parent" 26 android:layout_height="match_parent"> 27 28 <android.support.v7.widget.LinearLayoutCompat 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:fitsSystemWindows="true" 32 android:orientation="vertical" 33 android:paddingLeft="24dp" 34 android:paddingRight="24dp" 35 android:paddingTop="56dp"> 36 37 <android.support.v7.widget.AppCompatImageView 38 android:layout_width="150dp" 39 android:layout_height="150dp" 40 android:layout_gravity="center_horizontal" 41 android:layout_marginBottom="24dp" 42 android:src="@mipmap/ic_launcher" /> 43 44 <!--郵箱--> 45 <android.support.design.widget.TextInputLayout 46 android:layout_width="match_parent" 47 android:layout_height="wrap_content" 48 android:layout_marginBottom="8dp" 49 android:layout_marginTop="8dp"> 50 51 <android.support.design.widget.TextInputEditText 52 android:id="@+id/edit_sign_in_email" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:hint="郵箱" 56 android:inputType="textEmailAddress" 57 tools:ignore="HardcodedText" /> 58 </android.support.design.widget.TextInputLayout> 59 60 <!--密碼--> 61 <android.support.design.widget.TextInputLayout 62 android:layout_width="match_parent" 63 android:layout_height="wrap_content" 64 android:layout_marginBottom="8dp" 65 android:layout_marginTop="8dp"> 66 67 <android.support.design.widget.TextInputEditText 68 android:id="@+id/edit_sign_in_password" 69 android:layout_width="match_parent" 70 android:layout_height="wrap_content" 71 android:hint="密碼" 72 android:inputType="textPassword" 73 tools:ignore="HardcodedText" /> 74 </android.support.design.widget.TextInputLayout> 75 76 <android.support.v7.widget.AppCompatButton 77 android:id="@+id/btn_sign_in" 78 android:layout_width="match_parent" 79 android:layout_height="wrap_content" 80 android:layout_marginBottom="24dp" 81 android:layout_marginTop="24dp" 82 android:background="@android:color/holo_orange_dark" 83 android:gravity="center" 84 android:padding="12dp" 85 android:text="登錄" 86 android:textColor="@android:color/white" 87 tools:ignore="HardcodedText" /> 88 89 <android.support.v7.widget.AppCompatTextView 90 android:id="@+id/tv_link_sign_up" 91 android:layout_width="match_parent" 92 android:layout_height="wrap_content" 93 android:layout_marginBottom="24dp" 94 android:gravity="center" 95 android:text="還沒有賬戶?現在注冊吧" 96 android:textSize="16sp" 97 tools:ignore="HardcodedText" /> 98 99 <com.joanzapata.iconify.widget.IconTextView 100 android:id="@+id/icon_sign_in_wechat" 101 android:layout_width="100dp" 102 android:layout_height="100dp" 103 android:layout_gravity="center_horizontal" 104 android:gravity="center" 105 android:text="{fa-weixin}" 106 android:textColor="#04b00f" 107 android:textSize="40sp" /> 108 109 </android.support.v7.widget.LinearLayoutCompat> 110 </android.support.v4.widget.NestedScrollView> 111 </android.support.v7.widget.LinearLayoutCompat>
2.2 登錄的邏輯框架
1 package com.flj.latte.ec.sign; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.support.annotation.NonNull; 6 import android.support.annotation.Nullable; 7 import android.support.design.widget.TextInputEditText; 8 import android.util.Patterns; 9 import android.view.View; 10 import android.widget.Toast; 11 12 import com.flj.latte.delegates.LatteDelegate; 13 import com.diabin.latte.ec.R; 14 import com.diabin.latte.ec.R2; 15 import com.flj.latte.net.RestClient; 16 import com.flj.latte.net.callback.ISuccess; 17 import com.flj.latte.util.log.LatteLogger; 18 import com.flj.latte.wechat.LatteWeChat; 19 import com.flj.latte.wechat.callbacks.IWeChatSignInCallback; 20 21 import butterknife.BindView; 22 import butterknife.OnClick; 23 24 public class SignInDelegate extends LatteDelegate { 25 26 @BindView(R2.id.edit_sign_in_email) 27 TextInputEditText mEmail = null; 28 @BindView(R2.id.edit_sign_in_password) 29 TextInputEditText mPassword = null; 30 31 32 //點擊登錄按鈕 33 @OnClick(R2.id.btn_sign_in) 34 void onClickSignIn() { 35 if (checkForm()) { 36 37 } 38 } 39 40 //點擊微信第三方登錄 41 @OnClick(R2.id.icon_sign_in_wechat) 42 void onClickWeChat() { 43 44 } 45 46 //跳轉到注冊的鏈接 47 @OnClick(R2.id.tv_link_sign_up) 48 void onClickLink() { 49 50 getSupportDelegate().start(new SignUpDelegate()); 51 } 52 53 private boolean checkForm() { 54 final String email = mEmail.getText().toString(); 55 final String password = mPassword.getText().toString(); 56 57 boolean isPass = true; 58 59 if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 60 mEmail.setError("錯誤的郵箱格式"); 61 isPass = false; 62 } else { 63 mEmail.setError(null); 64 } 65 66 if (password.isEmpty() || password.length() < 6) { 67 mPassword.setError("請填寫至少6位數密碼"); 68 isPass = false; 69 } else { 70 mPassword.setError(null); 71 } 72 73 return isPass; 74 } 75 76 @Override 77 public Object setLayout() { 78 return R.layout.delegate_sign_in; 79 } 80 81 @Override 82 public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) { 83 84 } 85 }
【測試】
3.服務器數據簡單介紹
3.1 服務器的數據
【說明】提前准備的json數據
3.2 數據端訪問的數據
3.3 打印信息的級別類封裝
【源碼】com.flj.latte.util.log.LatteLogger
1 package com.flj.latte.util.log; 2 3 import com.orhanobut.logger.Logger; 4 5 /** 6 * Created by 傅令傑 on 2017/4/22 7 */ 8 9 public final class LatteLogger { 10 11 private static final int VERBOSE = 1; 12 private static final int DEBUG = 2; 13 private static final int INFO = 3; 14 private static final int WARN = 4; 15 private static final int ERROR = 5; 16 private static final int NOTHING = 6; 17 18 //控制log等級 19 private static int LEVEL = VERBOSE; 20 21 public static void v(String tag, String message) { 22 if (LEVEL <= VERBOSE) { 23 Logger.t(tag).v(message); 24 } 25 } 26 27 public static void d(String tag, Object message) { 28 if (LEVEL <= DEBUG) { 29 Logger.t(tag).d(message); 30 } 31 } 32 33 public static void d(Object message) { 34 if (LEVEL <= DEBUG) { 35 Logger.d(message); 36 } 37 } 38 39 public static void i(String tag, String message) { 40 if (LEVEL <= INFO) { 41 Logger.t(tag).i(message); 42 } 43 } 44 45 public static void w(String tag, String message) { 46 if (LEVEL <= WARN) { 47 Logger.t(tag).w(message); 48 } 49 } 50 51 public static void json(String tag, String message) { 52 if (LEVEL <= WARN) { 53 Logger.t(tag).json(message); 54 } 55 } 56 57 public static void e(String tag, String message) { 58 if (LEVEL <= ERROR) { 59 Logger.t(tag).e(message); 60 } 61 } 62 }
【測試】通過輸入登錄的信息,可以返回需要的數據;
4.與基於GreenDao的數據庫框架設計
【課程鏈接】https://www.imooc.com/learn/760
4.1 添加依賴和配置
4.2 創建entivity
【說明】【生成代碼數據的顯示】如果生成的代碼以文件的形式顯示出來,需要配置相應的代碼路徑;GreenDao的本意是不允許修改代碼,一般不要修改,修改之后會出現莫名其妙的問題;
【GreenDao生成代碼】
【說明】生成了很多的代碼;
【說明】GreenDao生成了一些DaoSession的內容,不要去修改;
4.3 openHelper類的創建
【說明】GreenDao在提供了openHelper類,但是在每次的APP打開之后,openHelper會將之前的APP的儲存的數據刪除掉,現在我們建立現在自己的配置類;
【說明】需要首先生成entivity之后才能書寫此類,因為DaoMaster是基於entivity的;
4.4 功能的抽取
【功能的抽取】-新建類
【單例模式】【惰性加載】使用單例模式,使用Holder惰性加載;
【構造方法不可見】
【數據庫的初始化】
4.5 數據的存儲
【測試】
4.6 添加數據反射顯示機制
【說明】簡化使用終端查看的繁瑣的步驟,使用facebook的依賴庫;原理也是抽象層,reactNative;
【功能】查看數據庫;將原生的界面映射到Web界面上;
【數據庫的數據的顯示】
【原生界面的顯示】
5.用戶狀態與用戶信息的回調封裝
【說明】我們需要用戶狀態的回調和用戶信息的回調;需要創建一些接口;
5.1 注冊的回調實例
【說明】登錄和注冊是登錄APP的唯一的接口,沒有必要分散在別的接口;在入口處理是最好的;
【增加注冊的回調】
【測試】【邏輯的使用】注冊成功,彈出土司;當然在此接口中可以做出一些其他的動作,比如發送統計信息等等;
5.2 登錄回調的實例
【測試】
【插件工具】
5.3 登錄注冊的封裝
【說明】登錄和注冊屬於一體的內容,不應該分開獨立,應該整合在一起;
【封裝登錄監聽監聽的接口】
【設置登錄監聽接口】
- onAttach方法
Fragment和Activity建立關聯的時候調用(獲得activity的傳遞的值)
【登錄成功/失敗的信息的保存】
【業務的調用】
【測試】
【增加跳轉頁面】
【測試】
【第一次,可以登錄成功了】
【第二次啟動,直接進入到信息頁面】
6. 框架的總結
【說明】看視頻的最后三分鍾;
【第一次打開APP】
【非第一次打開APP】
【記錄是否登錄/注冊成功】此時就可以判斷下次的啟動登陸的邏輯了;
【說明】書寫最少的代碼,完成最多的邏輯;最后在example中書寫的代碼的數量很少;