一、MVC
1.简介
MVC是目前大多数企业采用J2EE的结构设计,主要适用于交互式的Web应用。在Android中也有体现和使用,但是存在一定的弊端(下面将讲述),于是才有了Android官方推荐的MVP。

在Android的开发过程中,每个层对应如下:
Model层:对应Java Bean、Database、SharePreference和网络请求等;
View层:对应xml布局、自定义View或ViewGroup;
Controller层:对应Activity、Fragment;
2.实践
对于理论的理解 ,还是需要结合实际。下面我们将前面文章实现的https登录Demo,使用MVC的方式来进行重构:
项目结构:

View层:
activity_login.xml
1.简介
MVC是目前大多数企业采用J2EE的结构设计,主要适用于交互式的Web应用。在Android中也有体现和使用,但是存在一定的弊端(下面将讲述),于是才有了Android官方推荐的MVP。

在Android的开发过程中,每个层对应如下:
Model层:对应Java Bean、Database、SharePreference和网络请求等;
View层:对应xml布局、自定义View或ViewGroup;
Controller层:对应Activity、Fragment;
2.实践
对于理论的理解 ,还是需要结合实际。下面我们将前面文章实现的https登录Demo,使用MVC的方式来进行重构:
项目结构:

View层:
activity_login.xml
<?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.qunar.hotel.controller.LoginActivity">
<!--登录输入用户名-->
<com.qunar.hotel.view.LoginInputView
android:id="@+id/login_intput_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--登录输入密码-->
<com.qunar.hotel.view.LoginInputView
android:id="@+id/login_intput_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--登录按钮-->
<Button
android:id="@+id/login_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<!--登录结果文案-->
<TextView
android:id="@+id/login_result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
LoginInputView.java
public class LoginInputView extends LinearLayout {
private TextView title;
private EditText content;
public LoginInputView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.inputview_login, this);
title = (TextView) findViewById(R.id.input_title);
content = (