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模式进行开发的时候发现项目的结构模式对开发是有一定的 ...
随机推荐
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- python 读取 xlsx
>>> xl = pd.ExcelFile("dummydata.xlsx") >>> xl.sheet_names [u'Sheet1', u ...
- kong 安装
1. yum 参考信息 https://bintray.com/kong/kong-community-edition-rpm $ sudo yum install epel-release $ su ...
- eclipse marketplace网络连接失败的解决方法
2015-12-04 01:12:33 本想在eclipse上安装一个插件,点进help-EclipseMarketplace却连接失败,错误如下: 在help-instal new software ...
- guaua学习,工具专题
Preconditions 1,http://www.cnblogs.com/peida/p/Guava_Preconditions.html 1 .checkArgument(boolean) : ...
- Requirejs简单介绍
具体详情请进入官网查阅:http://requirejs.org 一.什么是Requirejs RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一. 二. ...
- mysql用触发器同步表
一.先复制表 : and DATE = '2016-09-26' or DATE = '2016-09-27'; 二.创建插入数据时的[触发器] [在phpmyadmin 运行时记得要修改语句定界符 ...
- Android.mk语法解析
Android.mk 相当于 Linux 中的 Makefile 文件,用来向安卓系统描述如何编译源代码.该文件会被编译器解析多次,所以尽量减少在 Android.mk 中声明变量. Android. ...
- Ubuntu secuerCRT连接失败,The remote system refused the connection.
新安装的ubuntu系统,securtCRT连接失败,出现下面结果,这是因为ubuntu没有安装工具. The remote system refused the connection. 解决办法: ...
- jeecg中List页面标签的用法
1.t:datagrid的常用属性 1. <t:datagrid name="jeecgDemoList" checkbox="true" sortNam ...