示例效果

一共三个控件,EditText,Button,TextView

成功显示账号信息,查询失败显示错误信息。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pureWhite"> <Button
android:id="@+id/btnQuery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="获取账号信息"
android:textColor="@color/pureWhite"
android:textSize="20dp"
android:onClick="ButtonClick"
android:theme="@style/GenericButtonStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etAccount" /> <EditText
android:id="@+id/etAccount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:ems="10"
android:hint="输入账号"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Name" /> <TextView
android:id="@+id/tvResult"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginTop="40dp"
tools:text="查询结果"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnQuery" /> </androidx.constraintlayout.widget.ConstraintLayout>

主界面代码

控件关键信息

注意:为了简化,这里查询数据库用的是模拟操作,随机成功或者失败。

不使用任何框架的传统做法

新建用户信息类Account和回调接口

public class Account {
private String name;
private int level;
public interface ResultCallback {
void onSuccess(Account account); void onFailure();
}

Activity中的代码

public class HelloActivity extends AppCompatActivity {
private TextView tvResult;
private EditText etAccount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tvResult = findViewById(R.id.tvResult);
etAccount = findViewById(R.id.etAccount);
} public void ButtonClick(View view) {
String userInput=getUserInput();
getAccountData(userInput, new ResultCallback() {
@Override
public void onSuccess(Account account) {
showSuccessPage(account);
} @Override
public void onFailure() {
showFailurePage();
}
});
} private String getUserInput() {
return etAccount.getText().toString();
} private void showSuccessPage(Account account) {
tvResult.setText("用户账号:"+account.getName()+"|"+
"用户等级:"+account.getLevel());
}
private void showFailurePage() {
tvResult.setText("获取数据失败");
} private void getAccountData(String accountName, ResultCallback cb) {
Random random=new Random();
boolean isSuccess=random.nextBoolean();
if (isSuccess) {
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
cb.onSuccess(account);
} else {
cb.onFailure();
}
} }

MVC

Account和ResultCallback同上

MVCModel代码

public class MVCModel {
public void getAccountData(String accountName, ResultCallback cb) {
Random random=new Random();
boolean isSuccess=random.nextBoolean();
if (isSuccess) {
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
cb.onSuccess(account);
} else {
cb.onFailure();
}
}
}

MVCActivity代码

public class MVCActivity extends AppCompatActivity {
private TextView tvResult;
private EditText etAccount;
private MVCModel model;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tvResult = findViewById(R.id.tvResult);
etAccount = findViewById(R.id.etAccount);
model=new MVCModel();
} public void ButtonClick(View view) {
String userInput=getUserInput();
model.getAccountData(userInput, new ResultCallback() {
@Override
public void onSuccess(Account account) {
showSuccessPage(account);
} @Override
public void onFailure() {
showFailurePage();
}
});
} private String getUserInput() {
return etAccount.getText().toString();
} private void showSuccessPage(Account account) {
tvResult.setText("用户账号:"+account.getName()+"|"+
"用户等级:"+account.getLevel());
}
private void showFailurePage() {
tvResult.setText("获取数据失败");
}
}

 MVC模式缺点

Controller View不能完全解耦。

Activity过于臃肿,需要承担部分业务代码。

Android MVC MVP MVVM (一)的更多相关文章

  1. android MVC && MVP && MVVM分析和对比

    相关:http://www.cnblogs.com/wytiger/p/5305087.html 出处http://blog.csdn.net/self_study,对技术感兴趣的同鞋加群544645 ...

  2. Android MVC,MVP,MVVM模式入门——重构登陆注册功能

    一  MVC模式: M:model,业务逻辑 V:view,对应布局文件 C:Controllor,对应Activity 项目框架: 代码部分: layout文件(适用于MVC和MVP两个Demo): ...

  3. Android MVC MVP MVVM (三)

    MVVM Model-View-ViewModel的简写 在MVP基础上实现数据视图的DataBinding,数据变化,视图自动变化,反之也成立. DataBinding 启用DataBinding ...

  4. Android MVC MVP MVVM (二)

    MVP模型 View主要是Activity,Fragment MVP和MVC的差别 1.Model和View不再直接通信,通过中间层Presenter来实现. 2.Activity的功能被简化,不再充 ...

  5. Android App的设计架构:MVC,MVP,MVVM与架构经验谈

    相关:http://www.cnblogs.com/wytiger/p/5996876.html 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开发中,很多人经常会头疼于 ...

  6. Android App的设计架构:MVC,MVP,MVVM与架构AAAAA

    1. 架构设计的目的1.1 通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.1.2 这样做的好处是使得程序在开发的过程中,开发人员只需要专注于一点,提高程序开发的效率,并且更容易进行后续 ...

  7. 用户界面编程模式 MVC MVP MVVM

    用户界面编程模式 MVC MVP MVVM 程序 = 数据 + 算法 数据:就是待处理的东西 算法:就是代码 涉及到人机交互的程序,不可避免涉及到界面和界面上显示的数据原始方式是界面代码和逻辑代码糅合 ...

  8. MVC, MVP, MVVM比较以及区别(上)

    MVC, MVP和MVVM都是用来解决界面呈现和逻辑代码分离而出现的模式.以前只是对它们有部分的了解,没有深入的研究过,对于一些里面的概念和区别也是一知半解.现在一边查资料,并结合自己的理解,来谈一下 ...

  9. MVC, MVP, MVVM比较以及区别

    MVC, MVP和MVVM都是用来解决界面呈现和逻辑代码分离而出现的模式.以前只是对它们有部分的了解,没有深入的研究过,对于一些里面的概念和区别也是一知半解.现在一边查资料,并结合自己的理解,来谈一下 ...

随机推荐

  1. 【Python之路】特别篇--服务商API认证、Restful、一致性哈希

    API加密方式 1/ 加密方式: Md5 (随机字符串 + 时间戳) 2/ 发送方式: http://127.0.0.1:8888/index?pid= MD5加密值 | 时间戳 | 序号 服务端接收 ...

  2. BZOJ 1257 [CQOI2007]余数之和 数学

    都不知道说什么好...咕咕到现在.. 求:$\sum_{i=1}^n \space k\space mod \space i$ 即求:$n*k-\sum_{i=1}^n\space \lfloor \ ...

  3. locate/find

    locate 从数据库 (/var/lib/mlocate/mlocate.db) 查找命令,使用updatedb更新库. 类似于数据库的索引建立,在首次简历索引的时候,很耗费资源,在建立完成后,查询 ...

  4. #define定义宏函数 的正确使用

    如何使用宏来定义一个自定义函数呢?首先我们来看下面这段代码 #define SQUARE(x) x*x int main() { int a = 5; printf("SQUARE(a): ...

  5. python 处理 json 四个函数dumps、loads、dump、load的区别

    1 .json.dumps() 函数是将一个 Python 数据类型列表(可以理解为字典)进行json格式的编码(转换成字符串,用于传播)eg, dict = {"age": &q ...

  6. synchronized的对象锁和类锁

    概念 synchronized 是 Java 中的关键字,是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机制, ...

  7. LGU67496 小$s$的玻璃弹珠

    题意 在一幢\(m\)层建筑你将获得\(n\)个一样的鸡蛋,从高于\(x\)的楼层落下的鸡蛋都会碎.如果一个蛋碎了,你就不能再把它掉下去. 你的目标是确切地知道\(x\)的值.问至少要扔几次才能确定. ...

  8. centos与debian网卡

    debian    /etc/network/interfaces # This file describes the network interfaces available on your sys ...

  9. 5.使用Ribbon实现客户端侧负载均衡

                                Ribbon实现客户端侧负载均衡 5.1. Ribbon简介 Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法 ...

  10. DDCTF-2018-writeup(5misc)

    打了好几天最后也只是80多名,我好菜啊.jpg 0x00  (╯°□°)╯︵ ┻━┻ 题目: (╯°□°)╯︵ ┻━┻ d4e8e1f4a0f7e1f3a0e6e1f3f4a1a0d4e8e5a0e6 ...