Android MVP设计模式登录具体实现Material Design风格

首先讲讲什么是MVP设计模式:

Model 依然是业务逻辑和实体模型
View 对应于Activity,负责View的绘制以及与用户交互
Presenter 负责完成View于Model间的交互

Android MVP设计模式登录具体实现Material Design风格_第1张图片

然后看看具体分包图

Android MVP设计模式登录具体实现Material Design风格_第2张图片

运行截图:

Android MVP设计模式登录具体实现Material Design风格_第3张图片

具体代码如下:

- M层

实体类UserBean.class

public class UserBean {

    public String username;

    public String password;
}

登录回调接口

public interface OnLoginListener {
    void getDataSuccess(UserBean userBean);
    void getDataFail(String msg);
}

IUserBiz.class

public interface IUserBiz {
    void login(String username,String password,OnLoginListener onLoginListener);
}

UserBiz.class

public class UserBiz implements IUserBiz {

    @Override
    public void login(final String username, final String password, final OnLoginListener onLoginListener) {
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if("kxx".equals(username) && "123".equals(password)){
                    UserBean userBean = new UserBean();
                    userBean.username = username;
                    userBean.password = password;
                    onLoginListener.getDataSuccess(userBean);
                }else {
                    onLoginListener.getDataFail("用户名或密码错误");
                }
            }
        }.start();
    }
}

- V层

ILoginView.class

public interface ILoginView {
    String getUserName();
    String getPassWord();

    void showLoading();
    void hideLoading();

    void showSuccessData(UserBean userBean);
    void showFailData(String msg);

    void clearData();
}

登录界面 LoginView.class

public class LoginView extends AppCompatActivity implements ILoginView,View.OnClickListener {

    private TextInputLayout tl_name;
    private TextInputEditText et_name;
    private TextInputEditText et_pwd;
    private Button bt_login;
    private Button bt_clear;

    private ProgressDialog dialog;
    private UserPresenter userPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();
    }

    private void initView() {
        tl_name = (TextInputLayout) findViewById(R.id.tl_name);
        et_name = (TextInputEditText) findViewById(R.id.et_name);
        et_pwd = (TextInputEditText) findViewById(R.id.et_pwd);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        bt_clear.setOnClickListener(this);

        userPresenter = new UserPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
//                et_name.setError("格式不正确");
//                tl_name.setError("格式不正确");
                userPresenter.login();
                break;
            case R.id.bt_clear:
                userPresenter.clear();
                break;
        }
    }

    @Override
    public String getUserName() {
        return et_name.getText().toString();
    }

    @Override
    public String getPassWord() {
        return et_pwd.getText().toString();
    }


    @Override
    public void showLoading() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("Loading...");
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if(dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @Override
    public void showSuccessData(UserBean userBean) {
        Toast.makeText(this, "@@@"+userBean.username+"---"+userBean.password, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailData(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void clearData() {
        et_name.setText("");
        et_pwd.setText("");
    }

布局文件

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .support.design.widget.TextInputLayout
        android:id="@+id/tl_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterMaxLength="11"
        app:counterEnabled="true"
        android:layout_margin="5dp"
        app:errorEnabled="true"
        >

        .support.design.widget.TextInputEditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="11"
            android:hint="请输入账号"
            />

    .support.design.widget.TextInputLayout>

    .support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:passwordToggleEnabled="true"
        >

        .support.design.widget.TextInputEditText
            android:id="@+id/et_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="11"
            android:inputType="textPassword"
            android:hint="请输入密码"/>

    .support.design.widget.TextInputLayout>

    

- P层

UserPresenter.class

public class UserPresenter {
    private LoginView loginView;

    private UserBiz userBiz;
    Handler handler = new Handler();

    public UserPresenter(LoginView loginView) {
        this.loginView = loginView;
        userBiz = new UserBiz();
    }

    public void login() {
        loginView.showLoading();

        userBiz.login(loginView.getUserName(), loginView.getPassWord(), new OnLoginListener() {
            @Override
            public void getDataSuccess(final UserBean userBean) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.showSuccessData(userBean);
                        loginView.hideLoading();
                    }
                });
            }

            @Override
            public void getDataFail(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.showFailData(msg);
                        loginView.hideLoading();
                    }
                });
            }


        });
    }
    public void clear() {
        loginView.clearData();
    }
}

源码下载地址:http://download.csdn.net/detail/codekxx/9833505

你可能感兴趣的:(Android)