android Mvp简单实用
View 对应于Activity,负责View的绘制以及与用户交互
Model 依然是业务逻辑和实体模型
Presenter 负责完成View于Model间的交互
模拟客户端的登录操作,并实现登录成功与登录失败的回调。
1.bean文件
1 package com.mvp.bean; 2 3 public class UserBean { 4 private String username; 5 private String pwd; 6 7 public UserBean() { 8 super(); 9 } 10 11 public UserBean(String username, String pwd) { 12 super(); 13 this.username = username; 14 this.pwd = pwd; 15 } 16 17 public String getUsername() { 18 return username; 19 } 20 21 public void setUsername(String username) { 22 this.username = username; 23 } 24 25 public String getPwd() { 26 return pwd; 27 } 28 29 public void setPwd(String pwd) { 30 this.pwd = pwd; 31 } 32 33 }
2.Model
1 package com.mvp.model; 2 3 import com.mvp.listener.OnLoginListener; 4 5 public interface UserModel { 6 // 登录 7 void Login(String username, String pwd,OnLoginListener listener); 8 }
3.Model的实现,OnLoginListener是成功、失败的回调
1 package com.mvp.model; 2 3 import android.annotation.SuppressLint; 4 5 import com.mvp.listener.OnLoginListener; 6 7 @SuppressLint("HandlerLeak") 8 public class UserModelImpl implements UserModel { 9 /** 10 * 实现业务操作 11 */ 12 @Override 13 public void Login(final String username, String pwd, 14 final OnLoginListener listener) { 15 new Thread() { 16 17 @Override 18 public void run() { 19 if (username.equals("1")) { 20 listener.onSuccess(); 21 } else if (username.equals("2")) { 22 listener.onError(); 23 } 24 } 25 }.start(); 26 } 27 }
4.UserView
1 package com.mvp.view; 2 3 public interface UserView { 4 5 String getUserName(); 6 7 String getPwd(); 8 9 void showDialog(String title); 10 }
5.Presenter
1 package com.mvp.presenter; 2 3 import android.os.Handler; 4 5 import com.mvp.listener.OnLoginListener; 6 import com.mvp.model.UserModel; 7 import com.mvp.model.UserModelImpl; 8 import com.mvp.view.UserView; 9 10 public class UserPresenter { 11 private UserModel userModel; 12 private UserView userView; 13 private Handler mHandler = new Handler(); 14 15 public UserPresenter(UserView userView) { 16 this.userView = userView; 17 userModel = new UserModelImpl(); 18 } 19 20 public void login(String username, String pwd) { 21 userModel.Login(username, pwd, new OnLoginListener() { 22 23 @Override 24 public void onSuccess() { 25 mHandler.post(new Runnable() { 26 27 @Override 28 public void run() { 29 userView.showDialog("success"); 30 } 31 }); 32 } 33 34 @Override 35 public void onError() { 36 mHandler.post(new Runnable() { 37 38 @Override 39 public void run() { 40 userView.showDialog("error"); 41 } 42 }); 43 } 44 }); 45 } 46 }
6.Activity文件
1 package com.mvp.view; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.Context; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.TextView; 12 13 import com.mvp.presenter.UserPresenter; 14 15 public class MainActivity extends Activity implements UserView, OnClickListener { 16 private TextView tv_show; 17 private EditText et_username, et_pwd; 18 private Button btn_save, btn_clear; 19 private UserPresenter userPresenter; 20 private Context context; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 initView(); 27 } 28 29 private void initView() { 30 userPresenter = new UserPresenter(this); 31 tv_show = (TextView) findViewById(R.id.tv_show); 32 et_username = (EditText) findViewById(R.id.et_username); 33 et_pwd = (EditText) findViewById(R.id.et_pwd); 34 findViewById(R.id.btn_save).setOnClickListener(this); 35 findViewById(R.id.btn_clear).setOnClickListener(this); 36 } 37 38 @Override 39 public void onClick(View v) { 40 switch (v.getId()) { 41 case R.id.btn_save: 42 userPresenter.login(getUserName(), getPwd()); 43 break; 44 case R.id.btn_clear: 45 break; 46 } 47 } 48 49 @Override 50 public String getUserName() { 51 return et_username.getText().toString(); 52 } 53 54 @Override 55 public String getPwd() { 56 return et_pwd.getText().toString(); 57 } 58 59 @Override 60 public void showDialog(String title) { 61 new AlertDialog.Builder(this).setTitle(title) 62 .setPositiveButton("确定", null).setNegativeButton("取消", null) 63 .show(); 64 } 65 }