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. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  2. Codeforces 559C Gerald and Giant Chess【组合数学】【DP】

    LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...

  3. BZOJ3673 可持久化并查集 by zky 【主席树】

    BZOJ3673 可持久化并查集 by zky Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a ...

  4. SpringBoot @ConfigurationProperties报错

    idea报错如下: Not registered via @EnableConfigurationProperties or marked as Spring component less... (C ...

  5. LeetCode 760. Find Anagram Mappings

    原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two lists Aand B, ...

  6. express中session的存储与销毁

    1.首先在使用session之前需要先配置session的过期时间等,在入口文件app.js中 app.use(express.session({ cookie: { maxAge: config.g ...

  7. spring boot 使用spring.resources.static-locations 分离系统模版&&资源文件

    方便我们将资源配置以及模版&&静态文件分离出来,而不是打包在一起,比如以下的一个demo 参考配置: server.port=8006 spring.application.name= ...

  8. 转 neighbour table overflow 问题解决

    接到保障,说某来机器服务没法访问,于是,准备连接到机器上去看个究竟. 尼玛居然连不上,连ping都ping不通,无奈只能求助机房. 机房人员检查, 发现报 neighbour table overfl ...

  9. Linux之 xstart调用 x11vnc远程图形化桌面

    问题:用 xmanager 中的 xstart 启动界面,报x11无法打开 . 1. root调整x11参数,将其打开[root@localhost ~]# vi /etc/ssh/sshd_conf ...

  10. 项目代码部署百度云(使用git部署,node环境)

    学习做了一个小demo,总是在自己的电脑,和局域网上运行很没意思,现在就做点有意思的事情,将代码部署百度云. 1)首先你得进入百度云(直接百度,先要注册一个账号) 2)点击那个“应用引擎”,就会进入 ...