一、Model
package com.czhappy.mvpdemo.model;/** * author: chenzheng * created on: 2019/5/16 11:06 * description: */public class User { public String username; public String password;}
package com.czhappy.mvpdemo.biz;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:08 * description: */public interface UserLoginListener { public void loginSuccess(User user); public void loginFailed();}
package com.czhappy.mvpdemo.biz;/** * author: chenzheng * created on: 2019/5/16 11:07 * description: */public interface IUserLoginBiz { public void login(String username, String password, UserLoginListener userLoginListener);}
package com.czhappy.mvpdemo.biz;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:10 * description: */public class UserLoginBiz implements IUserLoginBiz { @Override public void login(final String username, final String password, final UserLoginListener userLoginListener) { new Thread() { @Override public void run() { super.run(); try { Thread.sleep(2000); if ("admin".equals(username) && "123".equals(password)) { User user = new User(); user.username = username; user.password = password; userLoginListener.loginSuccess(user); } else { userLoginListener.loginFailed(); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }}
二、View
package com.czhappy.mvpdemo.view;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:26 * description: */public interface IUserLoginView { public void showLoading(); public void hideLoading(); public String getUsername(); public String getPassword(); public void toMainActivity(User user); public void showLoginFailed();}
package com.czhappy.mvpdemo.activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ProgressBar;import android.widget.Toast;import com.czhappy.mvpdemo.R;import com.czhappy.mvpdemo.model.User;import com.czhappy.mvpdemo.presenter.UserLoginPresenter;import com.czhappy.mvpdemo.view.IUserLoginView;/** * author: chenzheng * created on: 2019/5/16 10:41 * description: */public class LoginActivity extends AppCompatActivity implements IUserLoginView{ private EditText username_et, password_et; private Button login_btn; private ProgressBar progressBar; private UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); } private void initView() { username_et = findViewById(R.id.username_et); password_et = findViewById(R.id.password_et); login_btn = findViewById(R.id.login_btn); progressBar = findViewById(R.id.progressBar); login_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { userLoginPresenter.login(); } }); } @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideLoading() { progressBar.setVisibility(View.GONE); } @Override public String getUsername() { return username_et.getText().toString(); } @Override public String getPassword() { return password_et.getText().toString(); } @Override public void toMainActivity(User user) { Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show(); } @Override public void showLoginFailed() { Toast.makeText(this, "登录失败", Toast.LENGTH_LONG).show(); }}
三、Presenter
Presenter是用作Model和View之间交互的桥梁
package com.czhappy.mvpdemo.presenter;import android.os.Handler;import com.czhappy.mvpdemo.biz.IUserLoginBiz;import com.czhappy.mvpdemo.biz.UserLoginBiz;import com.czhappy.mvpdemo.biz.UserLoginListener;import com.czhappy.mvpdemo.model.User;import com.czhappy.mvpdemo.view.IUserLoginView;/** * author: chenzheng * created on: 2019/5/16 11:13 * description: */public class UserLoginPresenter { private IUserLoginBiz userLoginBiz; private IUserLoginView userLoginView; private Handler mHandler = new Handler(); public UserLoginPresenter(IUserLoginView userLoginView) { this.userLoginView = userLoginView; userLoginBiz = new UserLoginBiz(); } public void login(){ userLoginView.showLoading(); userLoginBiz.login(userLoginView.getUsername(), userLoginView.getPassword(), new UserLoginListener() { @Override public void loginSuccess(final User user) { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { userLoginView.toMainActivity(user); userLoginView.hideLoading(); } }); } @Override public void loginFailed() { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { userLoginView.showLoginFailed(); userLoginView.hideLoading(); } }); } }); }}