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. BZOJ5196: [Usaco2018 Feb]Taming the Herd(DP暴力)

    5196: [Usaco2018 Feb]Taming the Herd Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 78  Solved: 71[ ...

  2. JS 快速获取数据中的最大、最小值

    var  numbers = [5,23 , 1420 , 5215 , 228 , 400 , 105, 411]; var maxInNumbers = Math.max.apply(Math, ...

  3. Quartz 2D编程指南(2) - 图形上下文

    一个Graphics Context表示一个绘制目标.它包含绘制系统用于完成绘制指令的绘制参数和设备相关信息.Graphics Context定义了基本的绘制属性,如颜色.裁减区域.线条宽度和样式信息 ...

  4. VScode插件

    Visual Studio Code (简称 VS Code / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮.智能代码补全.自定义热键.括号匹配.代码片段. ...

  5. Prometheus 简介

    Prometheus 是一个开源的服务监控系统和时间序列数据库. 特性: 高维度数据模型 自定义查询语言 可视化数据展示 高效的存储策略 易于运维 提供各种客户端开发库 警告和报警 数据导出   gi ...

  6. 随着tomcat一起启动一个线程

    package test; import javax.servlet.*; public class MyCode implements ServletContextListener { //当Tom ...

  7. 杂项: Memcached

    ylbtech-杂项: Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动 ...

  8. SQL Server、Oracle和MySQL判断NULL的方法

    SQL Server.Oracle和MySQL判断NULL的方法 本文讲述SQL Server.Oracle.MySQL查出值为NULL的替换. 在SQL Server Oracle MySQL当数据 ...

  9. JAVA访问控制变量、类变量、类方法

    1.私有:同类中 2.默认:同包中的类 3.保护:同包中的类  子类中(继承性) 4.公有:无范围 创建子类并覆盖方法时,必须考虑原来方法的访问控制: 作为通用的规则,覆盖方法是,新方法的访问控制不能 ...

  10. oracle用户解锁

    创建一个概要文件 create profile frank_profile limit    SESSIONS_PER_USER  5    IDLE_TIME  2    FAILED_LOGIN_ ...