Mvp登录

一:View层定义成功失败的方法

public interface IView {

void one(String succss);

void eroor(String eroor);

}

二:P层传入账号密码

public interface IPresenter {

void komg(String name,String mima);

}

三:M层定义接口

public interface IModel {

interface MtoV{

void one(String succss);

void eroor(String eroor);

}

void kong(MtoV mtoV,String name,String mima);

}

四:M层实现接口判断密码

@Override

public void kong(MtoV mtoV, String name, String mima) {

if (name.equals("123")&&mima.equals("123")){

mtoV.one("登录成功");

}else {

mtoV.eroor("登录失败");

}

}

五:P层实现接口

private IModeliModel=new Model();

private IViewiView;

public Presenter(IView iView) {

this.iView = iView;

}

@Override

public void komg(String name,String mima) {

if (iView!=null){

iModel.kong(this,name,mima);

}

}

@Override

public void one(String succss) {

if (iView!=null){

iView.one(succss);

}

}

@Override

public void eroor(String eroor) {

if (iView!=null){

iView.eroor(eroor);

}

}

六:最后Activity中实现接口

方法中实现P层方法

IPresenter iPresenter=new Presenter(this);

iPresenter.komg(et1.getText().toString(),et2.getText().toString());

七:成功跳转,失败Toast

@Override

    public void one(String succss) {

Intent intent =new Intent(MainActivity.this, HelloActivity.class);

startActivity(intent);

}

@Override

    public void eroor(String eroor) {

Toast.makeText(this,"账号或密码有误", Toast.LENGTH_SHORT).show();

}

}

你可能感兴趣的:(Mvp登录)