码迷,mamicode.com
首页 > 移动开发 > 详细

android MVP模式

时间:2016-08-12 23:58:15      阅读:440      评论:0      收藏:0      [点我收藏+]

标签:

1、视图

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6 
 7     <EditText 
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:id="@+id/et_login_username"
11         />
12     <EditText 
13         android:layout_below="@id/et_login_username"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:id="@+id/et_login_password"
17         android:layout_marginTop="10dp"
18         android:password="true"
19         />
20     
21     <Button 
22         android:layout_width="90dp"
23         android:layout_height="wrap_content"
24         android:text="登陆"
25         android:id="@+id/btn_login_login"
26         android:layout_below="@id/et_login_password"
27         android:layout_marginTop="10dp"
28         android:layout_marginLeft="70dp"
29         android:layout_marginRight="10dp"
30         />
31     
32     <Button 
33         android:layout_width="90dp"
34         android:layout_height="wrap_content"
35         android:text="取消"
36         android:id="@+id/btn_login_clear"
37         android:layout_below="@id/et_login_password"
38         android:layout_marginTop="10dp"
39         android:layout_toRightOf="@id/btn_login_login"
40         />
41     
42     <ProgressBar 
43         android:id="@+id/progress_login"
44         android:layout_width="80dp"
45         android:layout_height="80dp"
46         android:layout_marginBottom="50dp"
47         android:layout_centerInParent="true"
48         android:background="#000000"
49         />
50 
51 </RelativeLayout>

2、Activity

 1 package com.zyhtest.zyhmvptest;
 2 
 3 import com.zyhtest.zyhmvptest.presenter.ILoginPresenter;
 4 import com.zyhtest.zyhmvptest.presenter.LoginPresenterCompl;
 5 import com.zyhtest.zyhmvptest.view.ILoginView;
 6 
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.ProgressBar;
13 import android.widget.Toast;
14 import android.app.Activity;
15 
16 public class LoginActivity extends Activity implements ILoginView, OnClickListener {
17     private EditText editUser;
18     private EditText editPass;
19     private Button btnLogin;
20     private Button btnClear;
21     private ProgressBar progressBar;
22     
23     private ILoginPresenter loginPresenter;
24     
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.login);
29         
30         editUser = (EditText) findViewById(R.id.et_login_username);
31         editPass = (EditText) findViewById(R.id.et_login_password);
32         btnLogin = (Button) findViewById(R.id.btn_login_login);
33         btnClear = (Button) findViewById(R.id.btn_login_clear);
34         progressBar = (ProgressBar) findViewById(R.id.progress_login);
35         
36         btnLogin.setOnClickListener(this);
37         btnClear.setOnClickListener(this);
38         
39         loginPresenter = new LoginPresenterCompl(this);
40         loginPresenter.setProgressBarVisibility(View.INVISIBLE);
41     }
42     
43     @Override
44     public void onClick(View v) {
45         switch(v.getId()){
46             case R.id.btn_login_clear:
47                 loginPresenter.clear();
48                 break;
49             case R.id.btn_login_login:
50                 loginPresenter.setProgressBarVisibility(View.VISIBLE);
51                 btnLogin.setEnabled(false);
52                 btnClear.setEnabled(false);
53                 loginPresenter.doLogin(editUser.getText().toString().trim(), editPass.getText().toString().trim());
54                 break;
55             default:
56                 break;
57         }
58     }
59     
60     @Override
61     public void clearText() {
62         editUser.setText("");
63         editPass.setText("");
64     }
65     
66     @Override
67     public void onLoginResult(Boolean result, int code) {
68         loginPresenter.setProgressBarVisibility(View.INVISIBLE);
69         btnLogin.setEnabled(true);
70         btnClear.setEnabled(true);
71         if(result){
72             Toast.makeText(this, "Login Success!", Toast.LENGTH_SHORT).show();
73         }else{
74             Toast.makeText(this, "Login Fail!", Toast.LENGTH_SHORT).show();
75         }
76     }
77     
78     @Override
79     public void onSetProgressBarVisibility(int visibility) {
80         progressBar.setVisibility(visibility);
81     }
82 
83 
84 }

3、MVP中的V

V接口

1 package com.zyhtest.zyhmvptest.view;
2 
3 public interface ILoginView {
4     public void clearText();
5     public void onLoginResult(Boolean result, int code);
6     public void onSetProgressBarVisibility(int visibility);
7 }

4、MVP中的P

接口

1 package com.zyhtest.zyhmvptest.presenter;
2 
3 public interface ILoginPresenter {
4     void clear();
5     void doLogin(String username, String password);
6     void setProgressBarVisibility(int visibility);
7 }

实现

 1 package com.zyhtest.zyhmvptest.presenter;
 2 
 3 import android.os.Handler;
 4 import android.os.Looper;
 5 
 6 import com.zyhtest.zyhmvptest.model.IUser;
 7 import com.zyhtest.zyhmvptest.model.UserModel;
 8 import com.zyhtest.zyhmvptest.view.ILoginView;
 9 
10 public class LoginPresenterCompl implements ILoginPresenter {
11     private ILoginView iLoginView;
12     private IUser user;
13     private Handler handler;
14     
15     public LoginPresenterCompl(ILoginView iLoginView){
16         this.iLoginView = iLoginView;
17         initUser();
18         handler = new Handler(Looper.getMainLooper());
19     }
20     
21     @Override
22     public void clear() {
23         iLoginView.clearText();
24     }
25 
26     @Override
27     public void doLogin(String username, String password) {
28         Boolean isLoginSuccess = true;
29         final int code = user.checkUserValidity(username, username);
30         if(code != 0) isLoginSuccess = false;
31         final Boolean result = isLoginSuccess;
32         handler.postDelayed(new Runnable(){
33             @Override
34             public void run() {
35                 iLoginView.onLoginResult(result, code);
36             }
37             
38         }, 3000);
39     }
40 
41     @Override
42     public void setProgressBarVisibility(int visibility) {
43         iLoginView.onSetProgressBarVisibility(visibility);
44     }
45     
46     private void initUser() {
47         user = new UserModel("mvp", "mvp");
48     }
49 
50 }

5、MVP中的M

接口

1 package com.zyhtest.zyhmvptest.model;
2 
3 public interface IUser {
4     public String getName();
5     public String getPasswd();
6     public int checkUserValidity(String name, String passwd);
7 }

实现

 1 package com.zyhtest.zyhmvptest.model;
 2 
 3 public class UserModel implements IUser {
 4     private String name;
 5     private String passwd;
 6     public UserModel(String name, String passwd){
 7         this.name   = name;
 8         this.passwd = passwd;
 9     }
10     @Override
11     public String getName() {
12         return name;
13     }
14 
15     @Override
16     public String getPasswd() {
17         return passwd;
18     }
19 
20     @Override
21     public int checkUserValidity(String name, String passwd) {
22         if(name == null || passwd == null || !name.equals(getName()) || !passwd.equals(getPasswd())){
23             return -1;
24         }
25         return 0;
26     }
27 
28 }

 

参考代码:https://github.com/kaedea/android-mvp-pattern/

文字说明:https://segmentfault.com/a/1190000003927200或者http://www.open-open.com/lib/view/open1446377609317.html

android MVP模式

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5766896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!