Android MVP模式简单易懂的介绍方式 (一)

Android MVP模式简单易懂的介绍方式 (二)

Android MVP模式简单易懂的介绍方式 (三)

讲完M和P,接下来就要讲V了。View也很好理解,就是把UI需要做的事情,通过接口暴露出来。那么UI要做什么事情,无非就是对UI控件的操作(这里是清空输入)、还有登陆成功或者失败的操作嘛。因此,我们可以这样写。

public interface ILoginView {

    void onClearText();
void onLoginReuslt(Boolean res,int code);
}

一样,有了接口,当然需要一个类来实现他。那么谁来实现这个接口呢?当然是我们的Activity。Activity实现了ILoginView接口后,由于逻辑的调用我们都在钱面写完了。因此,只需要写好UI处理即可。为了和ILoginPresenter 通讯,所以Activity需要一个ILoginPresenter ,然后,由ILoginPresenter 去和ILoginView进行通讯,从而实现UI与业务的分离

public class LoginActivity extends AppCompatActivity implements ILoginView,View.OnClickListener{

    private EditText et_name;
private EditText et_password;
ILoginPresenter iLoginPresenter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("登陆");
findViewById(R.id.bt_login).setOnClickListener(this);
findViewById(R.id.bt_clear).setOnClickListener(this);
et_name = findViewById(R.id.et_username);
et_password = findViewById(R.id.et_password); iLoginPresenter = new ILoginPresenterCompl(this);
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_clear:
iLoginPresenter.clear();
break;
case R.id.bt_login:
iLoginPresenter.doLogin(et_name.getText().toString(),et_password.getText().toString());
break;
default:
}
} @Override
public void onClearText() {
et_name.setText("");
et_password.setText("");
} @Override
public void onLoginReuslt(Boolean res, int code) {
switch (code){
case 0:
break;
case 1:
Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
break;
case -1:
Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show();
break;
case -2:
Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show();
break;
default:
}
} }

为了方便大家,顺便把布局文件也贴上,那么到这里,我们整一个MVP就完成了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.himarking.myapplication.MainActivity"> <LinearLayout
android:orientation="vertical"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_margin="10dp"
android:layout_marginTop="200dp"
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="10dp"
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> <LinearLayout
android:layout_marginTop="200dp"
android:layout_margin="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/bt_login"
android:text="登陆"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_clear"
android:text="清空"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>

Android MVP模式简单易懂的介绍方式 (三)的更多相关文章

  1. Android MVP模式简单易懂的介绍方式 (二)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 上一篇文章我们介绍完了Model的创建 ...

  2. Android MVP模式简单易懂的介绍方式 (一)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...

  3. Android MVP模式 简单易懂的介绍方式

    主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200

  4. MVP 模式简单易懂的介绍方式

    为什么用Android MVP 设计模式? 当项目越来越庞大.复杂,参与的研发人员越来越多的时候,MVP 模式 的优势就充分显示出来了. MVP 模式是 MVC 模式在 Android 上的一种变体, ...

  5. android MVP模式简单介绍

    原文 http://zhengxiaopeng.com/2015/02/06/Android%E4%B8%AD%E7%9A%84MVP/ 前言 MVP作为一种MVC的演化版本在Android开发中受到 ...

  6. Android MVP模式简单介绍:以一个登陆流程为例

    老的项目用的MVC的模式,最近完成了全部重构成MVP模式的工作,虽然比较麻烦,好处是代码逻辑更加清楚.简洁,流程更加清晰,对于后续版本迭代维护都挺方便.对于一些想要学习MVP模式的同学来讲,百度搜出来 ...

  7. Android MVP模式

    转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...

  8. android MVP模式介绍与实战

    android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...

  9. Android架构篇--MVP模式的介绍篇

    摘要: 在MVVM成熟之前MVP模式在Android上有被神化的趋势,笔者曾经在商业项目中从零开始大规模采用过MVP模式对项目进行开发.在使用MVP模式进行开发的时候发现项目的结构模式对开发是有一定的 ...

随机推荐

  1. matlab 相关系数的计算

    1. 首先说说自相关和互相关的概念.     这 个是信号分析里的概念,他们分别表示的是两个时间序列之间和同一个时间序列在任意两个不同时刻的取值之间的相关程度,即互相关函数是描述随机信号 x(t),y ...

  2. HDU - 4336:Card Collector(min-max容斥求期望)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  3. Python学习-str与byte类型以及编码

    Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和b ...

  4. bad ELF interpreter

    安装JDK的时候遇到的问题,  多半是64位的系统安装了32位的软件导致的

  5. vue插件vue-infinite-loading的使用

    vue-infinite-loading官网:https://peachscript.github.io/vue-infinite-loading/ 安装: npm install vue-infin ...

  6. 高并发的epoll+线程池,线程池专注实现业务

    我们知道,服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下 ...

  7. 含锂电池的 PCBA 运输快递时如何包装?

    含锂电池的 PCBA 运输快递时如何包装? PCBA 和电池必须固定. PCBA 和电池必须独立包装. 独立包装的外壳必须为硬包装,防止运输中挤压导致短路. 电池电量在 80% 或以下.

  8. 【转】MFC对话框和控件

    原文网址:http://www.cnblogs.com/tiwlin/archive/2013/05/08/3067966.html 对话框和控件 对话框是Windows应用程序中一种常用的资源,其主 ...

  9. js前台调用lodop打印

    lodop简单介绍 lodop的打印功能已经非常强大,也在带web端的图形界面,可以供用户使用.使用js在前台调用lodop打印,一般分为两种方法: 1:特殊的指令打印,这种打印方式,是采用的与js无 ...

  10. ZooKeeper+Kafka+Storm

    http://www.cnblogs.com/panfeng412/archive/2012/11/30/how-to-install-and-deploy-storm-cluster.html