首页 > 代码库 > Android MVP模式下的登录实现

Android MVP模式下的登录实现

项目结构:

package com.example.mvpdemo.view;/** *    基本的页面操作  * */public interface IBaseView {    //Toast形式提示    public void showMsg(String msg);    //加载提示框    public void    showLoading();    //隐藏加载提示框    public void hiddenLoading();    //页面跳转    public void jumpActivity();    //返回    public boolean back();}
package com.example.mvpdemo.view;/** *    登陆界面的View,继承基本的IBaseView,IBaseView对于Login界面操作已经足够,所以没有实质内容  * */public interface ILoginView extends IBaseView {}
package com.example.mvpdemo.model;/** * 最基本的模型 * */public interface IBaseModel {    public static final int STATUS_FAIL = -1;// 验证失败    public static final int STATUS_SUCCESS = 0;// 成功    public static final int STATUS_NORMAL = 1;// 正常状态    public static final int STATUS_VERIFY_ING = 2;// 正常状态}
package com.example.mvpdemo.model;/** * 登陆的数据模型 * */public interface ILoginModel extends IBaseModel{    /**     *      * @param username 用户名     * @param pwd    密码     * @param loginCallBack    登陆回调接口     */    public void login(String username,String pwd,ILoginCallBack loginCallBack);    /**     *    登陆回调接口及回调方法     */    interface ILoginCallBack {        public void onStatus();    }}
package com.example.mvpdemo.model.impl;import com.example.mvpdemo.model.ILoginModel;import android.os.AsyncTask;/** * 登陆模型的具体实现 * */public class LoginModelImpl implements ILoginModel {    public int status = ILoginModel.STATUS_NORMAL;    //记录登陆状态    public String msg;    //登陆操作后的提示语        @Override    public void login(final String username, final String pwd,            final ILoginCallBack loginCallBack) {        if (status == ILoginModel.STATUS_VERIFY_ING) {            return;        }            new AsyncTask<Void, Void, ILoginCallBack>() {                @Override                protected void onPreExecute() {                    // TODO Auto-generated method stub                    super.onPreExecute();                }                @Override                protected ILoginCallBack doInBackground(Void... params) {                    // TODO Auto-generated method stub                    //具体的业务实现                    if (username.equals("123") && pwd.equals("123")) {                        status = ILoginModel.STATUS_SUCCESS;                        msg = "登陸成功";                    } else {                        msg = "用户名或密码错误";                        status = ILoginModel.STATUS_FAIL;                    }                    return loginCallBack;                }                @Override                protected void onPostExecute(ILoginCallBack result) {                    // TODO Auto-generated method stub                    loginCallBack.onStatus();    //登陆成功后进行接口回调,告诉presenter对view操作进行界面操作                    super.onPostExecute(result);                }            }.execute();    }}
package com.example.mvpdemo.presenter;import com.example.mvpdemo.model.ILoginModel;import com.example.mvpdemo.model.ILoginModel.ILoginCallBack;import com.example.mvpdemo.model.impl.LoginModelImpl;import com.example.mvpdemo.view.ILoginView;/** *    登陆的Presenter */public class LoginPresenter {    LoginModelImpl loginModel;    ILoginView iLoginView;    public LoginPresenter(ILoginView iLoginView) {        super();        loginModel = new LoginModelImpl();        this.iLoginView = iLoginView;    }    public ILoginView getiLoginView() {        return iLoginView;    }    public void setiLoginView(ILoginView iLoginView) {        this.iLoginView = iLoginView;    }        /**     * 调用model进行数据处理,根据回调接口来操作ILoginView进行对应的activity界面更新     * @param username     * @param pwd     */    public void login(String username, String pwd) {        System.out.println(username+","+pwd);        loginModel.login(username, pwd, new ILoginCallBack() {            public void onStatus() {                System.out.println(loginModel.status+"");                switch (loginModel.status) {                case LoginModelImpl.STATUS_SUCCESS:                    iLoginView.jumpActivity();                    iLoginView.showMsg(loginModel.msg);                    break;                case LoginModelImpl.STATUS_FAIL:                    iLoginView.showMsg(loginModel.msg);                    break;                default:                    break;                }                loginModel.status=ILoginModel.STATUS_NORMAL;            }        });    }}
package com.example.mvpdemo;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.example.mvpdemo.presenter.LoginPresenter;import com.example.mvpdemo.view.ILoginView;public class LoginActivity extends Activity implements ILoginView {    String Tag = "MainActivity";    LoginPresenter loginPresenter;    EditText username_et;    EditText pwd_et;    Button submit_btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        loginPresenter = new LoginPresenter(this);        initView();    }    void initView() {        username_et = (EditText) findViewById(R.id.username_et);        pwd_et = (EditText) findViewById(R.id.pwd_et);        submit_btn = (Button) findViewById(R.id.submit_btn);        submit_btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                String username = username_et.getText().toString();                String pwd = pwd_et.getText().toString();                loginPresenter.login(username, pwd);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void showMsg(String msg) {        // TODO Auto-generated method stub        Log.i(Tag, msg);        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();    }    @Override    public void showLoading() {        // TODO Auto-generated method stub        Log.i(Tag, "showLoading");    }    @Override    public void hiddenLoading() {        // TODO Auto-generated method stub        Log.i(Tag, "hiddenLoading");    }    @Override    public void jumpActivity() {        // TODO Auto-generated method stub        startActivity(new Intent(LoginActivity.this, MainActivity.class));    }    long lastTime;    @Override    public boolean back() {        long currTime=System.currentTimeMillis();        if(currTime-lastTime>3000){            showMsg("再按一次退出");            lastTime=currTime;        }else{            finish();        }        return false;    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        // TODO Auto-generated method stub        if(keyCode==KeyEvent.KEYCODE_BACK){            return back();        }        return super.onKeyDown(keyCode, event);    }}

 

Android MVP模式下的登录实现