model
package com.bawei.mymvp.mvp2;
import android.os.SystemClock;
public class NetUtils {
public static boolean loginApi(User user){
SystemClock.sleep(2000);
if(user.getName().equals("dj") && user.getPw().equals("123456")){
//TODO:这里最终需用真正的接口
return true;
}
return false;
}
}
///
package com.bawei.mymvp.mvp2;
public class User {
private String name;
private String pw;
public User(String name, String pw) {
this.name = name;
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
}
view
package com.bawei.mymvp.mvp2.view;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.mymvp.R;
import com.bawei.mymvp.mvp2.presemter.LoginPresenter;
import com.bawei.mymvp.mvp2.User;
public class MvpActivity extends Activity implements View.OnClickListener, IView {
EditText mEtName, mEtPw;
LoginPresenter mLoginPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvp);
initView();
//创建一个Presenter实例
initPresenter();
}
/**
* 绑定Presenter
*/
private void initPresenter() {
//把view传给presenter进行绑定
mLoginPresenter = new LoginPresenter(this);
}
private void initView() {
mEtName = findViewById(R.id.et_name);
mEtPw = findViewById(R.id.et_pw);
mEtPw.invalidate();
findViewById(R.id.button_login).setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button_login:
User user = new User(mEtName.getText().toString(), mEtPw.getText().toString());
//通过presenter的实例,调用presenter中的方法
mLoginPresenter.submit(user);
break;
default:
break;
}
}
/**
* 在Activity結束的時候解綁Presenter
*/
@Override
protected void onDestroy() {
super.onDestroy();
mLoginPresenter.detachView();
}
@Override
public void success(Object data) {
//接受到了结果,进行数据展示
Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
}
@Override
public void fail(String msg) {
//接受到了结果,进行数据展示
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
package com.bawei.mymvp.mvp2.view;
/**
* view层接口,内部两个回调方法
* 第一个成功,第二个失败
* @param <T>
*/
public interface IView<T> {
void success(T data);
void fail(String msg);
}
presenter
package com.bawei.mymvp.mvp2.presemter;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.bawei.mymvp.mvp2.NetUtils;
import com.bawei.mymvp.mvp2.User;
import com.bawei.mymvp.mvp2.view.IView;
public class LoginPresenter {
/**
* 持有view的实例
*/
private IView mIView;
public LoginPresenter(@NonNull IView iView) {
mIView = iView;
}
public void submit(User user){
if(checkName(user.getName()) && checkPw(user.getPw())){
//进行网络请求
boolean loginResult = NetUtils.loginApi(user);
//拿到结果后
if (loginResult){
//通过view的实例,把数据回调给view
mIView.success("");
}else{
//通过view的实例,把数据回调给view
mIView.fail("失败");
}
}else{
//通过view的实例,把数据回调给view
mIView.fail("用户名密码错");
}
}
public void detachView(){
mIView = null;
}
private boolean checkName(String name){
return !TextUtils.isEmpty(name);
}
private boolean checkPw(String pw){
return (!TextUtils.isEmpty(pw) && pw.length()>=6);
}
}