用户登录(Material Design + Data-Binding + MVP架构模式)实现
转载请注明出处: http://www.cnblogs.com/cnwutianhao/p/6772759.html
MVP架构模式 大家都不陌生,Google 也给出过相应的参考 Sample,
但是有的人会有疑问为啥 GitHub 上面大神写的 MVP架构模式 和 Google 的不太一样。
Google MVP架构模式 Sample 地址 https://github.com/googlesamples/android-architecture/tree/todo-mvp/
下面我们就仿照 Google 的 Sample 实现用户登录
项目结构:

示例演示图:

代码实现:
1.导入必要的开源库
示例项目采用 Material Design + Data-Binding + MVP
android {
...
// Data Binding
// https://developer.android.google.cn/topic/libraries/data-binding/index.html
dataBinding {
enabled true
}
}
dependencies {
...
// UI
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
}
2.Base类
1) BaseActivity
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
dataProcess();
}
protected abstract void initView();
protected abstract void initData();
protected abstract void dataProcess();
}
2) BasePresenter
public class BasePresenter {
}
3) BaseView
public interface BaseView {
}
4) IBasePresenter
public interface IBasePresenter {
void onDestroyView();
}
3.定义一个契约类,连接 P层 和 V层。这样可以使接口一目了然
在这里我要说明一下,用户登录界面,最基本的需要判断用户名是否为空,密码是否为空。
public class LoginContract {
interface loginView extends BaseView {
void accountIsNull();
void passWordIsNull();
void loginSuccess();
}
interface loginPresenter extends IBasePresenter {
void login(String account, String password);
}
}
4.定义一个 LoginPresenter ,判断条件在这里实现
public class LoginPresenter extends BasePresenter implements LoginContract.loginPresenter {
private LoginContract.loginView mView;
public LoginPresenter(LoginContract.loginView view) {
mView = view;
}
@Override
public void onDestroyView() {
mView = null;
}
@Override
public void login(String account, String password) {
if (TextUtils.isEmpty(account)) {
mView.accountIsNull();
return;
}
if (TextUtils.isEmpty(password)) {
mView.passWordIsNull();
return;
}
mView.loginSuccess();
}
}
5.定义一个 LoginActivity ,可以视其为 View层
public class LoginActivity extends BaseActivity implements LoginContract.loginView {
private Context mContext;
private LoginPresenter mLoginPresenter;
private ActivityLoginBinding mBinding;
@Override
protected void initView() {
mContext = LoginActivity.this;
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
}
@Override
protected void initData() {
mLoginPresenter = new LoginPresenter(this);
mBinding.loginBtn.setOnClickListener(this);
mBinding.loginChangePassword.setOnClickListener(this);
mBinding.loginRegister.setOnClickListener(this);
}
@Override
protected void dataProcess() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_btn:
mLoginPresenter.login(
mBinding.loginAccount.getText().toString(),
mBinding.loginPassword.getText().toString());
break;
default:
break;
}
}
@Override
public void accountIsNull() {
Toast.makeText(mContext, "请输入您的账户", Toast.LENGTH_LONG).show();
}
@Override
public void passWordIsNull() {
Toast.makeText(mContext, "请输入您的密码", Toast.LENGTH_LONG).show();
}
@Override
public void loginSuccess() {
Intent intentRegister = new Intent();
intentRegister.setClass(LoginActivity.this, MainActivity.class);
startActivity(intentRegister);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
}
6.布局代码
布局里涉及到 layout(Data-Binding)、CardView(Material Design)、TextInputLayout(Material Design)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_image"> <android.support.v7.widget.CardView
style="@style/cardElevation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="7dp"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
style="@style/TextStyle.Heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="40dp"
android:text="登录账号"
android:textAllCaps="true"
android:textSize="20sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical"> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="账号"
android:textColorHint="@color/gray"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"> <android.support.design.widget.TextInputEditText
android:id="@+id/login_account"
style="@style/TextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/input_border_bottom"
android:cursorVisible="true"
android:gravity="center|left|bottom"
android:inputType="textEmailAddress"
android:maxLength="50"
android:paddingBottom="10dp"
android:textColor="@color/black_effective"
android:textSize="18sp" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:textColorHint="@color/gray"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText
android:id="@+id/login_password"
style="@style/TextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/input_border_bottom"
android:cursorVisible="true"
android:gravity="center|left|bottom"
android:inputType="textPassword"
android:maxLength="50"
android:paddingBottom="10dp"
android:textColor="@color/black_effective"
android:textSize="18sp" /> </android.support.design.widget.TextInputLayout> <Button
android:id="@+id/login_btn"
style="@style/Button.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:padding="10dp"
android:text="登录"
android:textSize="18dp" />
</LinearLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="35dp"> <TextView
android:id="@+id/login_change_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="修改密码" /> <TextView
android:id="@+id/login_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="注册账号" />
</RelativeLayout> </LinearLayout> </android.support.v7.widget.CardView>
</RelativeLayout> </layout>
示例Sample下载:Material Design风格登录界面
7.总结
MVP架构模式 作为 MVC架构模式 的替代产物,是当今 Android开发 的趋势。Google 都在推荐开发者去用这种模式,作为开发者没有理由拒绝。
现在生产出来的安卓手机我觉得99.9%的系统都是Android 5.0+,所以开发者们更应该多了解Material Design。而不是做个页面像Android 2.x 甚至 1.x的样式。
Data-Binding 是 Google 推荐开发者使用的替代 findViewById 的产物。
总之一句话,Google官方推荐,就是开发者们要在App中重点使用的技术,早晚都要使用,你不用,不会用,就要被会用的人淘汰。
关注我的新浪微博,请认准黄V认证,获取最新安卓开发资讯。
关注科技评论家,领略科技、创新、教育以及最大化人类智慧与想象力!
用户登录(Material Design + Data-Binding + MVP架构模式)实现的更多相关文章
- 开发 Material Design+RxJava+Retrofit+MVP App 参考资料
前言 在开发一个基于 Material Design+RxJava+Retrofit+MVP 框架的 App 过程中学习的资料整理 —— 由G军仔分享 这里记录了我开发 大象 项目时,所学习的 ...
- iOS - MVP 架构模式
1.MVP 从字面意思来理解,MVP 即 Modal View Presenter(模型 视图 协调器),MVP 实现了 Cocoa 的 MVC 的愿景.MVP 的协调器 Presenter 并没有对 ...
- MVP架构模式
概念解释 MVP是Model(数据) View(界面) Presenter(表现层)的缩写,它是MVC架构的变种,强调Model和View的最大化解耦和单一职责原则 Model:负责数据的来源和封装, ...
- 设计模式笔记之一:MVP架构模式入门(转)
写在前面:昨天晚上,公司请来专家讲解了下MVP,并要求今后各自负责的模块都要慢慢的转到MVP模式上来.以前由于能力有限,没有认真关注过设计模式.框架什么的,昨晚突然兴趣大发,故这两天空闲时间一直在学习 ...
- 死磕安卓前序:MVP架构探究之旅—基础篇
前言 了解相关更多技术,可参考<我就死磕安卓了,怎么了?>,接下来谈一谈我们来学习一下MVP的基本认识. 大家对MVC的架构模式再熟悉不过.今天我们就学习一下MVP架构模式. MVC和MV ...
- 一文彻底搞清楚 Material Design
一文彻底搞清楚 Material Design 首先声明以下介绍的关于 Material Design 的介绍,都是基于在 Android 环境下,其实 Material Design 是一种为了让 ...
- Android Data Binding高级用法-Observable、动态生成Binding Class(三)
设置View的id 虽然说Data Binding这种分层模式使得我们对数据的传递简单明了,一般情况下我们可以不设置View的id,不使用findViewById即可对View进行数据上一系列的操作, ...
- android -------- Data Binding的使用(一)
Google推出自己官方的数据绑定框架Data Binding Library 已经很久了,很多企业也在使用 面试的时候也有问到,所以也去学习了一番,特来分享一下,希望对各位有所帮助 描述: Data ...
- Android MVP开发模式及Retrofit + RxJava封装
代码已上传到Github,因为接口都是模拟无法进行测试,明白大概的逻辑就行了! 欢迎浏览我的博客--https://pushy.site 1. MVP模式 1.1 介绍 如果熟悉MVP模式架构的话,对 ...
随机推荐
- SQLSERVER2008 错误18456
我遇到的问题,已经解决,如果你遇到不能解决可以咨询我 1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密 ...
- ios录音Demo
<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> ...
- JSP +++SERVIET总复习
一. JSP基础概念 软件架构 B/S架构:Browser/Server,浏览器-服务器 最大的优点就是:一次部署,处处访问. C/S架构:Client/Server,客户端-服务器 功能.事件丰富, ...
- 安装rabbitmq以及集群配置
前言: (一些有用没用的唠叨,反正看了也不少肉,跳过也没啥) 情况是这样的:虚拟机.CentOS 6.5.免编译包安装rabbitmq集群,可不用连外网. 我原计划是安装在虚拟机上wyt1/wyt2/ ...
- ubuntu 切换java环境,配置单独的用户环境
执行命令:sudo update-alternatives --config javaThere are 2 choices for the alternative java (providing ...
- Android开发之旅:环境搭建
1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD
- 使用javascript解一道关于会议日程安排的面试题
这道面试题是从 HarrisonHao 的一篇博文中看到的:原文链接 我看到之后,感觉此题十分有趣,遂自己用 node.js 以不同的思路实现了一遍,实现中使用了 lodash 原题比较长,而且是英文 ...
- Android自学反思总结(上)
从接触Android到现在有几个月的时间了,基本全部都是靠自学,从大一上学期学习完c语言,接着利用寒假时间和开学一个月左右的时间自学完javase,接着在导员的督促下,开始了Android学习之旅,现 ...
- download 下载文件 IE兼容性处理
根据CANIUSE(http://caniuse.com/#search=download)download兼容性如下图所示: 如上图所示,IE浏览器是不支持的. 1.测试代码: <!docty ...
- 老司机实战Windows Server Docker:4 单节点Windows Docker服务器简单运维(下)
上篇中,我们主要介绍了使用docker-compose对Windows Docker单服务器进行远程管理,编译和部署镜像,并且设置容器的自动启动.但是,还有一些重要的问题没有解决,这些问题不解决,就完 ...