登录代码mvp架构的具体实现,分享给大家。
mainactivity.java
public class mainactivity extends appcompatactivity implements view.onclicklistener, mvp.fff { private edittext musername; private edittext mpassword; private button login; private progressdialog mprogressdialog; private mvp mmvp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); mmvp = new mvp(this); } private void initview() { musername = (edittext) findviewbyid(r.id.username); mpassword = (edittext) findviewbyid(r.id.password); login = (button) findviewbyid(r.id.login); login.setonclicklistener(this); //动态的创建一个进度条 mprogressdialog = new progressdialog(this); } @override public void onclick(view view) { switch (view.getid()){ case r.id.login: string user = musername.gettext().tostring(); string pass = mpassword.gettext().tostring(); final user users = new user(); users.username=user; users.password=pass; boolean empty = mmvp.isempty(users); if (empty){ mprogressdialog.show(); mmvp.islogin(users); }else { toast.maketext(mainactivity.this, "输入不能为空", toast.length_short).show(); return; } break; } } @override public void success() { runonuithread(new runnable() { @override public void run() { mprogressdialog.dismiss(); toast.maketext(mainactivity.this, "欢迎回来,思密达", toast.length_short).show(); } }); } @override public void doil() { runonuithread(new runnable() { @override public void run() { mprogressdialog.dismiss(); toast.maketext(mainactivity.this, "登录失败,账号密码有问题", toast.length_short).show(); } }); } @override protected void ondestroy() { super.ondestroy(); mmvp=null; } }
userlogin.java
public class userlogin { //mvc controller层 public boolean login(user users){ systemclock.sleep(1000); if ("frq".equals(users.username)&&"frq".equals(users.password)){ return true; }else { return false; } } }
mvp.java
public class mvp { //mvp precenter层 public mvp(fff fff) { mfff = fff; } public boolean isempty(user users){ if (textutils.isempty(users.username) || textutils.isempty(users.password)){ return false; }else { return true; } } public void islogin(final user users){ new thread(){ @override public void run() { userlogin login = new userlogin(); boolean b = login.login(users); if (b){ mfff.success(); }else { mfff.doil(); } } }.start(); } public interface fff{ void success(); void doil(); } private fff mfff; } =========================================== public class precenter { public precenter(fff fff) { mfff = fff; } public void as(string urlstring){ new asynctask<string, void, arraylist<firstbean.resultbean>>() { @override protected arraylist<firstbean.resultbean> doinbackground(string... strings) { string s = new httpurl().tostring(strings[0]); //调用外部类 firstbean firstbean = new gson().fromjson(s, firstbean.class); //gson解析 if (firstbean!=null){ return (arraylist<firstbean.resultbean>) firstbean.getresult(); } return null; } @override protected void onpostexecute(arraylist<firstbean.resultbean> resultbeans) { super.onpostexecute(resultbeans); if (resultbeans!=null){ mfff.success(resultbeans); }else { mfff.bai(); } } }.execute(urlstring); } public interface fff{ void success(arraylist<firstbean.resultbean> resultbeans); void bai(); }; private fff mfff; }
以上就是android开发分享Android登录代码MVP架构详解的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/901598.html