MVPHelper插件的使用
(320条消息) MVPHelper更新日志 --- 新增常规分包模式_mvp怎么分包合理_三精-大精wing的博客-CSDN博客
实例
1.LoginContractor 将三个接口合并为一个
public interface LoginContract { interface Model { void login(String name, String psw, LoginPresenter loginPresenter); } interface View { void shouToast(String msg); void onSuccess(); String getName(); String getPassword(); } interface Presenter { void onSuccess();//登陆成功 void onFailure(String msg);//登陆失败 } }
2.LoginModel 对数据进行校验 进行业务逻辑判断
public class LoginModel implements LoginContract.Model { @Override public void login(String name, String psw, LoginPresenter loginPresenter) { if (name.equals("123") && psw.equals("123")) { loginPresenter.onSuccess(); } else { loginPresenter.onFailure("用户名或密码错误"); } } }
3.View 实现数据的获取
public class MainActivity extends AppCompatActivity implements LoginContract.View{ EditText name; EditText password; Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { name = findViewById(R.id.name); password=findViewById(R.id.password); login=findViewById(R.id.login); login.setOnClickListener(v -> { LoginPresenter presenter = new LoginPresenter(); presenter.setView(this); LoginContract.Model model = new LoginModel(); model.login(getName(), getPassword(), presenter); }); } @Override public void shouToast(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override public void onSuccess() { Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); } @Override public String getName() { return name.getText().toString(); } @Override public String getPassword() { return password.getText().toString(); } }
4 LoginPresenter 持有view
public class LoginPresenter implements LoginContract.Presenter { LoginModel model; LoginContract.View view; public void setView(LoginContract.View view) { this.view = view; } @Override public void onSuccess() { view.onSuccess(); } @Override public void onFailure(String msg) { view.shouToast(msg); } }
(318条消息) android学习笔记-MVP实例_android mvp 实例_有希N的博客-CSDN博客
(320条消息) Android 使用MVPHelper工具生成,MVPDemo模拟登录演示_WL-鬼的博客-CSDN博客
标签:mvp,架构,String,void,Override,LoginContract,public,name From: https://www.cnblogs.com/wanglongjiang/p/17346844.html