android--re-explore MVP mode

Source: Internet
Author: User

1, the previous article we have learned about the approximate MVP model, and then through this login function example, we can more easily grasp the MVP mode, and then to add some idle digression:MVP is a variant of MVC, is actually an upgrade. To say MVP is to say that MVC, in the MVC activity is actually the view level, but usually in the use of activity is the view is also controller, and does not have the view layer and controller layer separation, the coupling degree greatly improved, very not Beneficial to project management. The MVP abstracts the UI logic in the activity into the view interface, abstracting the business logic into the presenter interface, the model class or the original model.

2, let's take a look at our simple layout file, two input boxes and two buttons.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.qianmo.mvpdemo.MainActivity "> <edittext android:id=" @+id/edittext_u Ser "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignpar Entend= "true" android:layout_alignparentleft= "true" android:layout_alignparentright= "true" Android:la Yout_alignparentstart= "true" android:layout_alignparenttop= "true" android:hint= "Username"/> <editte XT android:id= "@+id/edittext_pass"        Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignend= "@+ Id/edittext_user "android:layout_alignparentleft=" true "android:layout_alignparentstart=" true "Androi d:layout_alignright= "@+id/edittext_user" android:layout_below= "@+id/edittext_user" android:hint= "Password"/&    Gt <button android:id= "@+id/button_clean" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_alignparentleft=" true "android:layout_alignparentstart=" true "Android:la yout_below= "@+id/edittext_pass" android:layout_marginleft= "35DP" android:layout_marginstart= "35DP" an        droid:layout_margintop= "69DP" android:text= "clean"/> <button android:id= "@+id/button_login" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbottom= "@+id/bu      Tton_clean "  Android:layout_alignend= "@+id/edittext_pass" android:layout_alignright= "@+id/edittext_pass" android:layout _marginend= "42DP" android:layout_marginright= "42DP" android:text= "Login"/></relativelayout>

In the creation of our feature class Logincontract, which contains this login function, view, presenter, model class, it is recommended to use the Mvphelper plugin, one step

Package com.qianmo.mvpdemo.contract;/** * Created by Wangjitao on 2016/10/31 0031. * Implementation class for implementing the Login function */public class Logincontract {public    interface View {public        void Doclean ();        public void Loginresult (Boolean issuccess, String messing);    }    Public interface Presenter {public        void clean ();        public void Dologin (string username, string password);    }    Public interface Model {    }}

Model Implementation Class

Package Com.qianmo.mvpdemo.model;import com.qianmo.mvpdemo.contract.logincontract;/** * Created by MVPHelper on 2016/ 10/31 */public class Loginmodelimpl implements Logincontract.model {    private String username;    private String password;    Public String GetUserName () {        return username;    }    public void Setusername (String username) {        this.username = username;    }    Public String GetPassword () {        return password;    }    public void SetPassword (String password) {        this.password = password;    }}

Implementation class of Persenter

Package Com.qianmo.mvpdemo.presenter;import Com.qianmo.mvpdemo.mainactivity;import com.qianmo.mvpdemo.contract.logincontract;/** * Created by Mvphelper on 2016/10/31 */public class Loginpresenterimpl Implements Logincontract.presenter {    private mainactivity mainactivity;    Public Loginpresenterimpl (mainactivity mainactivity) {        this.mainactivity = mainactivity;    }    @Override public    void Clean () {        mainactivity.doclean ();    }    @Override public    void Dologin (string username, string password) {        if ("Wangjitao". Equals (username) && "123". Equals (password)) {            Mainactivity.loginresult (True, "User:" + username + ", pass:" + password);        } else {            Mainactivity.loginresult (false, "User:" + username + ", pass:" + password);}}}    

And finally our UI, which is our activity.

Package Com.qianmo.mvpdemo;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.Toast;import Com.qianmo.mvpdemo.contract.logincontract;import Com.qianmo.mvpdemo.presenter.loginpresenterimpl;public Class Mainactivity extends Appcompatactivity implements Logincontract.view, View.onclicklistener {private EditText Et_userna    Me    Private EditText Et_password;    Private Button Btn_clean;    Private Button Btn_login;    Private Loginpresenterimpl Mloginpresenter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Et_username = (EditText) Findviewbyid (R.id.edittext_user);        Et_password = (EditText) Findviewbyid (R.id.edittext_pass);        Btn_clean = (Button) Findviewbyid (R.id.button_clean); Btn_login = (Button) Findviewbyid (r.id.button_lOgin);        Mloginpresenter = new Loginpresenterimpl (this);        Btn_login.setonclicklistener (this);    Btn_clean.setonclicklistener (this);        } @Override public void Doclean () {Et_username.settext ("");    Et_password.settext (""); } @Override public void Loginresult (Boolean issuccess, String messing) {if (issuccess) {toast.ma        Ketext (Mainactivity.this, "Landing success:" + Messing, Toast.length_short). Show ();        } else {Toast.maketext (mainactivity.this, "Login Failed", Toast.length_short). Show ();                }} @Override public void OnClick (view view) {switch (View.getid ()) {r.id.button_clean:                Mloginpresenter.clean ();            Break Case R.id.button_login:mloginpresenter.dologin (Et_username.gettext (). toString (), Et_password.gettext (). ToS                Tring ());        Break }    }}

OK, so it feels like you're going to write the MVP demo for a while.

 

  

 

android--re-explore MVP mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.