标签:tco add 业务逻辑 stat 登陆 get encoding void ase
1,上一篇我们已经了解了大致的MVP模式,下面再通过这个登录功能的例子 ,我们能过更加容易的掌握MVP模式,再来补充一些闲外话:MVP 是 MVC 的变种,其实是一种升级。要说 MVP 就要说说 MVC,在 MVC 中 Activity 其实是 View层级,但是通常在使用中 Activity即是View也是Controller,并没有将 View层 和 Controller层 进行分离, 耦合度大大提高,非常不利于项目的管理。MVP 把 Activity 中的 UI逻辑 抽象成 View接口,把 业务逻辑 抽象成 Presenter接口,Model类 还是原来的 Model。
2,先来看一下我们这次的简单的布局文件,两个输入框和两个按钮
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.qianmo.mvpdemo.MainActivity"> <EditText android:id="@+id/editText_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:hint="Username"/> <EditText android:id="@+id/editText_pass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/editText_user" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/editText_user" android:layout_below="@+id/editText_user" android:hint="Password"/> <Button android:id="@+id/button_clean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/editText_pass" android:layout_marginLeft="35dp" android:layout_marginStart="35dp" android:layout_marginTop="69dp" android:text="clean"/> <Button android:id="@+id/button_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button_clean" android:layout_alignEnd="@+id/editText_pass" android:layout_alignRight="@+id/editText_pass" android:layout_marginEnd="42dp" android:layout_marginRight="42dp" android:text="Login"/> </RelativeLayout>
在创建我们的功能类LoginContract,包含这个登录功能的,view、presenter、model类,这里推荐使用MVPHelper插件,一步搞定
package com.qianmo.mvpdemo.contract; /** * Created by wangjitao on 2016/10/31 0031. * 用于实现登录功能的实现类 */ public class LoginContract { public interface View { public void doClean(); public void loginResult(boolean isSuccess, String messing); } public interface Presenter { public void clean(); public void doLogin(String username, String password); } public interface Model { } }
model实现类
package com.qianmo.mvpdemo.model; import com.qianmo.mvpdemo.contract.LoginContract; /** * Created by MVPHelper on 2016/10/31 */ public class LoginModelImpl implements LoginContract.Model { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
persenter的实现类
package com.qianmo.mvpdemo.presenter; import com.qianmo.mvpdemo.MainActivity; import com.qianmo.mvpdemo.contract.LoginContract; /** * Created by MVPHelper on 2016/10/31 */ public class LoginPresenterImpl implements LoginContract.Presenter { private MainActivity mainActivity; public LoginPresenterImpl(MainActivity mainActivity) { this.mainActivity = mainActivity; } @Override public void clean() { mainActivity.doClean(); } @Override public void doLogin(String username, String password) { if ("wangjitao".equals(username) && "123".equals(password)) { mainActivity.loginResult(true, "user:" + username + ",pass:" + password); }else { mainActivity.loginResult(false, "user:" + username + ",pass:" + password); } } }
最后是我们的UI,也就是我们的Activity
package com.qianmo.mvpdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.qianmo.mvpdemo.contract.LoginContract; import com.qianmo.mvpdemo.presenter.LoginPresenterImpl; public class MainActivity extends AppCompatActivity implements LoginContract.View, View.OnClickListener { private EditText et_username; private EditText et_password; private Button btn_clean; private Button btn_login; private LoginPresenterImpl mLoginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText) findViewById(R.id.editText_user); et_password = (EditText) findViewById(R.id.editText_pass); btn_clean = (Button) findViewById(R.id.button_clean); btn_login = (Button) findViewById(R.id.button_login); mLoginPresenter = new LoginPresenterImpl(this); btn_login.setOnClickListener(this); btn_clean.setOnClickListener(this); } @Override public void doClean() { et_username.setText(""); et_password.setText(""); } @Override public void loginResult(boolean isSuccess, String messing) { if (isSuccess) { Toast.makeText(MainActivity.this, "登陆成功:" + messing, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show(); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_clean: mLoginPresenter.clean(); break; case R.id.button_login: mLoginPresenter.doLogin(et_username.getText().toString(), et_password.getText().toString()); break; } } }
ok,这样的话 感觉一会儿就写出MVP的小demo了
标签:tco add 业务逻辑 stat 登陆 get encoding void ase
原文地址:http://www.cnblogs.com/wjtaigwh/p/6016798.html