【0106】【项目实战】-【Android通用框架设计与完整电商APP开发】-【5】【登录、注册功能开发(ORM框架-GreenDao)】


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中书写的代码的数量很少;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM