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模式简单易懂的介绍方式 (三)的更多相关文章
- Android MVP模式简单易懂的介绍方式 (二)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 上一篇文章我们介绍完了Model的创建 ...
- Android MVP模式简单易懂的介绍方式 (一)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...
- Android MVP模式 简单易懂的介绍方式
主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200
- MVP 模式简单易懂的介绍方式
为什么用Android MVP 设计模式? 当项目越来越庞大.复杂,参与的研发人员越来越多的时候,MVP 模式 的优势就充分显示出来了. MVP 模式是 MVC 模式在 Android 上的一种变体, ...
- android MVP模式简单介绍
原文 http://zhengxiaopeng.com/2015/02/06/Android%E4%B8%AD%E7%9A%84MVP/ 前言 MVP作为一种MVC的演化版本在Android开发中受到 ...
- Android MVP模式简单介绍:以一个登陆流程为例
老的项目用的MVC的模式,最近完成了全部重构成MVP模式的工作,虽然比较麻烦,好处是代码逻辑更加清楚.简洁,流程更加清晰,对于后续版本迭代维护都挺方便.对于一些想要学习MVP模式的同学来讲,百度搜出来 ...
- Android MVP模式
转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...
- android MVP模式介绍与实战
android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...
- Android架构篇--MVP模式的介绍篇
摘要: 在MVVM成熟之前MVP模式在Android上有被神化的趋势,笔者曾经在商业项目中从零开始大规模采用过MVP模式对项目进行开发.在使用MVP模式进行开发的时候发现项目的结构模式对开发是有一定的 ...
随机推荐
- Java并发--同步容器
为了方便编写出线程安全的程序,Java里面提供了一些线程安全类和并发工具,比如:同步容器.并发容器.阻塞队列.Synchronizer(比如CountDownLatch).今天我们就来讨论下同步容器. ...
- Atcoder Beginner Contest 118
A: B +/- A 签到题. B: Foods Loved by Everyone 签到题. C: Monsters Battle Royale 怪物的血量一直两两相减,类似于辗转相减法. 可以证明 ...
- 查询避免Unknown column ‘xxx’ in ‘where clause’
但凡写过sql语句的人估计都曾经碰到过类似于Unknown column ‘xxx’ in ‘where clause’的问题. 单从字面理解,我们很容易得出列名不存在的结论,但是,很多时候起始并不是 ...
- 《selenium2 python 自动化测试实战》(18)——自动化测试模型(一)
线性测试 已经被淘汰了:线性测试就是一个脚本完成一个场景,代码基本没有复用,每一个脚本都要从头开始写——这哪行. 模块化与类库 这个就是分模块:有点类似面系那个对象,把功能(比如登录)单独拿出来,当下 ...
- JAVA card 应用开发(一) 创建第一个APPLET
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/freudlv/article/details/26499817 本文讲述在Eclipse环境下.怎样 ...
- Docker生态概览
百花齐放的容器技术 虽然 docker 把容器技术推向了巅峰,但容器技术却不是从 docker 诞生的.实际上,容器技术连新技术都算不上,因为它的诞生和使用确实有些年头了.下面的一串名称肯能有的你都没 ...
- 理解AI的角度
<经济学人>去年出了一期很经典的封面,封面里将全球各大高科技平台企业如谷歌.亚马逊之许描绘成正在采油的钻井,寓意很明显,在数字经济时代,大平台正在开采数字化的石油——大数据,而开采出来的大 ...
- wordpress插件汉化包,和使用教程
点击下载汉化包 解压后上传到该插件的 languages 目录即可
- Window下SVN使用总结
1 地址:http://subversion.apache.org/packages.html#windows 找到windows下的svn客户端工具.选择Win32Svn 进行安装. 一般环境变量会 ...
- 老齐python-基础7(文件操作、迭代)
在python3中,没有file这个内建类型了(python2中,file是默认类型) 1.读文件 创建文件,130.txt 并在里面输入 learn python http://qiwsir.git ...